Skip to content

Commit

Permalink
Add test for EventPickerTable, move non sense function from tnoodle a…
Browse files Browse the repository at this point in the history
…pi to wcif helper, remove unused functions
  • Loading branch information
campos20 committed Sep 14, 2020
1 parent 0a50fd2 commit 93a7830
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 69 deletions.
17 changes: 0 additions & 17 deletions tnoodle-ui/src/main/api/tnoodle.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ let bestMbldAttemptEndpoint = "/frontend/mbld/best";
let wcaEventsEndpoint = "/frontend/data/events";
let formatsEndpoint = "/frontend/data/formats";

export const copiesExtensionId =
"org.worldcubeassociation.tnoodle.SheetCopyCount";

export const fetchZip = (wcif, mbld, password, translations) => {
let payload = {
wcif,
Expand Down Expand Up @@ -46,20 +43,6 @@ export const fetchRunningVersion = () => {
return fetch(baseUrl + versionEndpoint);
};

/**
* This is the default extension object the backend expects
* @param {} copies
*/
export const getDefaultCopiesExtension = (copies = 1) => {
return {
id: copiesExtensionId,
specUrl: "",
data: {
numCopies: copies,
},
};
};

export const fetchAvailableFmcTranslations = () => {
return fetch(baseUrl + fmcTranslationsEndpoint);
};
Expand Down
2 changes: 1 addition & 1 deletion tnoodle-ui/src/main/components/EventPicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { updateWcaEvent, updateFileZipBlob } from "../redux/ActionCreators";
import {
getDefaultCopiesExtension,
copiesExtensionId,
} from "../api/tnoodle.api";
} from "../helper/wcif.helper";
import MbldDetail from "./MbldDetail";
import FmcTranslationsDetail from "./FmcTranslationsDetail";
import "./EventPicker.css";
Expand Down
5 changes: 3 additions & 2 deletions tnoodle-ui/src/main/components/EventPickerTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ const EventPickerTable = connect(
}
})
.then((availableTranslations) => {
if (!availableTranslations) {
// TODO evaluate impact of this. It's probably a useless check
/*if (!availableTranslations) {
return;
}
}*/
let translations = Object.keys(availableTranslations).map(
(translationId) => ({
id: translationId,
Expand Down
2 changes: 1 addition & 1 deletion tnoodle-ui/src/main/constants/default.wcif.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDefaultCopiesExtension } from "../api/tnoodle.api";
import { getDefaultCopiesExtension } from "../helper/wcif.helper";

// Add 1 round of 3x3x3
let default333 = {
Expand Down
6 changes: 0 additions & 6 deletions tnoodle-ui/src/main/functions/wca.helper.js

This file was deleted.

15 changes: 0 additions & 15 deletions tnoodle-ui/src/main/functions/wcif.functions.js

This file was deleted.

15 changes: 15 additions & 0 deletions tnoodle-ui/src/main/helper/wcif.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const copiesExtensionId =
"org.worldcubeassociation.tnoodle.SheetCopyCount";
/**
* This is the default extension object the backend expects
* @param {} copies
*/
export const getDefaultCopiesExtension = () => {
return {
id: copiesExtensionId,
specUrl: "",
data: {
numCopies: 1,
},
};
};
2 changes: 1 addition & 1 deletion tnoodle-ui/src/main/redux/Reducers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ActionTypes } from "./Types";
import { defaultWcif } from "../constants/default.wcif";
import { MBLD_DEFAULT } from "../constants/wca.constants";
import { getDefaultCopiesExtension } from "../api/tnoodle.api";
import { getDefaultCopiesExtension } from "../helper/wcif.helper";
import { competitionName2Id } from "../util/competition.name.util";

const defaultStore = {
Expand Down
128 changes: 128 additions & 0 deletions tnoodle-ui/src/test/EventPickerTable.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import React from "react";
import { act } from "react-dom/test-utils";

import { render, unmountComponentAtNode } from "react-dom";

import { Provider } from "react-redux";
import { createStore } from "redux";
import { Reducer } from "../main/redux/Reducers";
import { updateCompetitionId } from "../main/redux/ActionCreators";

import EventPickerTable from "../main/components/EventPickerTable";

import { events, languages } from "./mock/tnoodle.api.mock";
import { competitions } from "./mock/wca.api.mock";

import {
updateEditingStatus,
updateWcaEvent,
} from "../main/redux/ActionCreators";

import { getDefaultCopiesExtension } from "../main/helper/wcif.helper";

const tnoodleApi = require("../main/api/tnoodle.api");

let container = null;
beforeEach(() => {
// setup a DOM element as a render target
container = document.createElement("div");
document.body.appendChild(container);
});

afterEach(() => {
// cleanup on exiting
unmountComponentAtNode(container);
container.remove();
container = null;
});

it("Show editing warn if case of competition selected", async () => {
const store = createStore(Reducer);

jest.spyOn(
tnoodleApi,
"fetchAvailableFmcTranslations"
).mockImplementation(() =>
Promise.resolve(new Response(JSON.stringify(languages)))
);

jest.spyOn(tnoodleApi, "fetchWcaEvents").mockImplementation(() =>
Promise.resolve(new Response(JSON.stringify(events)))
);

// Choose a competition
const competitionId = competitions[0].id;
store.dispatch(updateCompetitionId(competitionId));

// Disable editing
store.dispatch(updateEditingStatus(true));

// Add one more round
const newEvent = {
id: "222",
rounds: [
{
id: "222-r1",
format: "a",
scrambleSetCount: 5,
extensions: [getDefaultCopiesExtension()],
},
],
};
store.dispatch(updateWcaEvent(newEvent));

// Render component
await act(async () => {
render(
<Provider store={store}>
<EventPickerTable />
</Provider>,
container
);
});

const paragraphs = Array.from(container.querySelectorAll("p"));

// Plural since we have 2 events
expect(paragraphs[0].innerHTML).toContain("events ");

// Show link to edit events
const link = paragraphs[1].querySelector("a");
expect(link.href).toContain(
`https://www.worldcubeassociation.org/competitions/${competitionId}/events/edit`
);

// Disabled events should not appear
const tables = Array.from(container.querySelectorAll("table"));
expect(tables.length).toBe(store.getState().wcif.events.length);
});

it("Singular event", async () => {
const store = createStore(Reducer);

jest.spyOn(tnoodleApi, "fetchWcaEvents").mockImplementation(() =>
Promise.resolve(new Response(JSON.stringify(events)))
);

// Choose a competition
const competitionId = competitions[0].id;
store.dispatch(updateCompetitionId(competitionId));

// Disable editing
store.dispatch(updateEditingStatus(true));

// Render component
await act(async () => {
render(
<Provider store={store}>
<EventPickerTable />
</Provider>,
container
);
});

const paragraphs = Array.from(container.querySelectorAll("p"));

// Singular 1 events
expect(paragraphs[0].innerHTML).toContain("event ");
});
27 changes: 1 addition & 26 deletions tnoodle-ui/src/test/Main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {

import Main from "../main/components/Main";

import { events } from "./mock/tnoodle.api.mock";
import { events, languages } from "./mock/tnoodle.api.mock";

const tnoodleApi = require("../main/api/tnoodle.api");

Expand Down Expand Up @@ -42,31 +42,6 @@ it("There should be only 1 button of type submit", async () => {
m: { name: "Mean of 3", shortName: "Mo3" },
};

const languages = {
da: "Danish",
de: "German",
en: "English",
es: "Spanish",
et: "Estonian",
fi: "Finnish",
fr: "French",
hr: "Croatian",
hu: "Hungarian",
id: "Indonesian",
it: "Italian",
ja: "Japanese",
ko: "Korean",
pl: "Polish",
pt: "Portuguese",
"pt-BR": "Portuguese (Brazil)",
ro: "Romanian",
ru: "Russian",
sl: "Slovenian",
vi: "Vietnamese",
"zh-CN": "Chinese (China)",
"zh-TW": "Chinese (Taiwan)",
};

// Turn on mocking behavior
jest.spyOn(tnoodleApi, "fetchWcaEvents").mockImplementation(() =>
Promise.resolve(new Response(JSON.stringify(events)))
Expand Down
25 changes: 25 additions & 0 deletions tnoodle-ui/src/test/mock/tnoodle.api.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,28 @@ export const events = [
is_multiple_blindfolded: true,
},
];

export const languages = {
da: "Danish",
de: "German",
en: "English",
es: "Spanish",
et: "Estonian",
fi: "Finnish",
fr: "French",
hr: "Croatian",
hu: "Hungarian",
id: "Indonesian",
it: "Italian",
ja: "Japanese",
ko: "Korean",
pl: "Polish",
pt: "Portuguese",
"pt-BR": "Portuguese (Brazil)",
ro: "Romanian",
ru: "Russian",
sl: "Slovenian",
vi: "Vietnamese",
"zh-CN": "Chinese (China)",
"zh-TW": "Chinese (Taiwan)",
};

0 comments on commit 93a7830

Please sign in to comment.