Skip to content

Commit

Permalink
Migrated tests to use immutable.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher-Chianelli committed Nov 17, 2020
1 parent 02bb62c commit 5c97f69
Show file tree
Hide file tree
Showing 55 changed files with 471 additions and 360 deletions.
2 changes: 1 addition & 1 deletion optaweb-employee-rostering-frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import { SpinnerIcon } from '@patternfly/react-icons';
import './index.css';
import { I18nextProvider } from 'react-i18next';
import { configureStore } from 'store';
import { List } from 'immutable';
import App from './ui/App';

// import i18n (needs to be bundled)
import i18n from './i18n';
import { List } from 'immutable';

const path = window.location.pathname;
let windowTenantId = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as tenantOperations from 'store/tenant/operations';
import { onPost } from 'store/rest/RestTestUtils';
import { alert } from 'store/alert';
import { doNothing } from 'types';
import { List } from 'immutable';
import { mockStore } from '../mockStore';
import { AppState } from '../types';
import * as adminOperations from './operations';
Expand Down Expand Up @@ -51,7 +52,7 @@ describe('Contract operations', () => {
const state: Partial<AppState> = {
tenantData: {
currentTenantId: 0,
tenantList: [],
tenantList: List(),
timezoneList: ['America/Toronto'],
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { withElement, withoutElementWithId } from 'util/ImmutableCollectionOperations';
import { ServerSideExceptionInfo, BasicObject } from 'types';
import { List } from 'immutable';
import { mockStore } from '../mockStore';
import { AppState } from '../types';
import * as actions from './actions';
Expand All @@ -23,15 +23,15 @@ import { AlertInfo, AlertComponent } from './types';

const state: Partial<AppState> = {
alerts: {
alertList: [{
alertList: List([{
id: 0,
createdAt: new Date(),
i18nKey: 'alert1',
variant: 'info',
variant: 'info' as 'info',
params: {},
components: [],
componentProps: [],
}],
}]),
idGeneratorIndex: 1,
},
};
Expand Down Expand Up @@ -162,12 +162,14 @@ describe('Alert reducers', () => {
it('add an alert', () => {
expect(
reducer(state.alerts, actions.addAlert(addedAlert)),
).toEqual({ idGeneratorIndex: 2, alertList: withElement(storeState.alerts.alertList, { ...addedAlert, id: 1 }) });
).toEqual({ idGeneratorIndex: 2, alertList: storeState.alerts.alertList.push({ ...addedAlert, id: 1 }) });
});

it('remove an alert', () => {
expect(
reducer(state.alerts, actions.removeAlert(removedAlertId)),
).toEqual({ ...state.alerts, alertList: withoutElementWithId(storeState.alerts.alertList, removedAlertId) });
).toEqual({ ...state.alerts,
alertList: storeState.alerts.alertList
.filterNot(a => a.id === removedAlertId) });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import { ActionType, AlertList, AlertAction } from './types';
import { List } from 'immutable';
import { ActionType, AlertList, AlertAction } from './types';

export const initialState: AlertList = {
alertList: List(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
*/

import { alert } from 'store/alert';
import {
createIdMapFromList, mapWithElement, mapWithoutElement,
mapWithUpdatedElement,
} from 'util/ImmutableCollectionOperations';
import { createIdMapFromList } from 'util/ImmutableCollectionOperations';
import { onGet, onPost, onDelete } from 'store/rest/RestTestUtils';
import { Contract } from 'domain/Contract';
import { List } from 'immutable';
import { mockStore } from '../mockStore';
import { AppState } from '../types';
import * as actions from './actions';
Expand All @@ -29,8 +27,8 @@ import reducer, { contractSelectors, contractOperations } from './index';
const state: Partial<AppState> = {
contractList: {
isLoading: false,
contractMapById: new Map([
[0, {
contractMapById: createIdMapFromList([
{
tenantId: 0,
id: 0,
version: 0,
Expand All @@ -39,8 +37,8 @@ const state: Partial<AppState> = {
maximumMinutesPerWeek: null,
maximumMinutesPerMonth: null,
maximumMinutesPerYear: null,
}],
[1, {
},
{
tenantId: 0,
id: 1,
version: 0,
Expand All @@ -49,8 +47,8 @@ const state: Partial<AppState> = {
maximumMinutesPerWeek: 100,
maximumMinutesPerMonth: null,
maximumMinutesPerYear: null,
}],
[2, {
},
{
tenantId: 0,
id: 2,
version: 0,
Expand All @@ -59,7 +57,7 @@ const state: Partial<AppState> = {
maximumMinutesPerWeek: null,
maximumMinutesPerMonth: null,
maximumMinutesPerYear: 100,
}],
},
]),
},
};
Expand Down Expand Up @@ -220,19 +218,19 @@ describe('Contract reducers', () => {
expect(
reducer(state.contractList, actions.addContract(addedContract)),
).toEqual({ ...state.contractList,
contractMapById: mapWithElement(storeState.contractList.contractMapById, addedContract) });
contractMapById: storeState.contractList.contractMapById.set(addedContract.id as number, addedContract) });
});
it('remove contract', () => {
expect(
reducer(state.contractList, actions.removeContract(deletedContract)),
).toEqual({ ...state.contractList,
contractMapById: mapWithoutElement(storeState.contractList.contractMapById, deletedContract) });
contractMapById: storeState.contractList.contractMapById.delete(deletedContract.id as number) });
});
it('update contract', () => {
expect(
reducer(state.contractList, actions.updateContract(updatedContract)),
).toEqual({ ...state.contractList,
contractMapById: mapWithUpdatedElement(storeState.contractList.contractMapById, updatedContract) });
contractMapById: storeState.contractList.contractMapById.set(updatedContract.id as number, updatedContract) });
});
it('refresh contract list', () => {
expect(
Expand Down Expand Up @@ -272,12 +270,12 @@ describe('Contract selectors', () => {
...storeState,
contractList: { ...storeState.contractList, isLoading: true },
});
expect(contractList).toEqual([]);
expect(contractList).toEqual(List());
});

it('should return a list of all contracts', () => {
const contractList = contractSelectors.getContractList(storeState);
expect(contractList).toEqual(expect.arrayContaining([
expect(contractList.toArray()).toEqual(expect.arrayContaining([
{
tenantId: 0,
id: 0,
Expand Down Expand Up @@ -309,6 +307,6 @@ describe('Contract selectors', () => {
maximumMinutesPerYear: 100,
},
]));
expect(contractList.length).toEqual(3);
expect(contractList.size).toEqual(3);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
* limitations under the License.
*/

import {
createIdMapFromList
} from 'util/ImmutableCollectionOperations';
import { createIdMapFromList } from 'util/ImmutableCollectionOperations';
import DomainObjectView from 'domain/DomainObjectView';
import { Contract } from 'domain/Contract';
import { ActionType, ContractList, ContractAction } from './types';
import { Map } from 'immutable';
import { ActionType, ContractList, ContractAction } from './types';

export const initialState: ContractList = {
isLoading: true,
Expand All @@ -32,7 +30,7 @@ const contractReducer = (state = initialState, action: ContractAction): Contract
case ActionType.SET_CONTRACT_LIST_LOADING: {
return { ...state, isLoading: action.isLoading };
}
case ActionType.ADD_CONTRACT, ActionType.UPDATE_CONTRACT: {
case ActionType.ADD_CONTRACT: case ActionType.UPDATE_CONTRACT: {
return { ...state, contractMapById: state.contractMapById.set(action.contract.id as number, action.contract) };
}
case ActionType.REMOVE_CONTRACT: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/
import { Contract } from 'domain/Contract';
import { AppState } from '../types';
import { Map, List } from 'immutable';
import { AppState } from '../types';

export const getContractById = (state: AppState, id: number): Contract => {
if (state.contractList.isLoading) {
Expand All @@ -38,6 +38,6 @@ export const getContractList = (state: AppState): List<Contract> => {

oldContractMapById = state.contractList.contractMapById;
contractListForOldContractMapById = out;

return out;
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
*/

import { alert } from 'store/alert';
import {
createIdMapFromList, mapWithElement, mapWithoutElement,
mapWithUpdatedElement,
} from 'util/ImmutableCollectionOperations';
import { createIdMapFromList, mapDomainObjectToView } from 'util/ImmutableCollectionOperations';
import { onGet, onPost, onDelete, onUploadFile } from 'store/rest/RestTestUtils';
import { Employee } from 'domain/Employee';
import * as skillActions from 'store/skill/actions';
import * as contractActions from 'store/contract/actions';
import { List } from 'immutable';
import { mockStore } from '../mockStore';
import { AppState } from '../types';
import * as actions from './actions';
Expand All @@ -31,8 +29,8 @@ import reducer, { employeeSelectors, employeeOperations } from './index';
const state: Partial<AppState> = {
employeeList: {
isLoading: false,
employeeMapById: new Map([
[1, {
employeeMapById: createIdMapFromList([
{
tenantId: 0,
id: 1,
version: 0,
Expand All @@ -41,8 +39,8 @@ const state: Partial<AppState> = {
contract: 1,
shortId: 'e1',
color: '#FFFFFF',
}],
[2, {
},
{
tenantId: 0,
id: 2,
version: 0,
Expand All @@ -51,13 +49,13 @@ const state: Partial<AppState> = {
contract: 1,
shortId: 'e2',
color: '#FFFFFF',
}],
},
]),
},
contractList: {
isLoading: false,
contractMapById: new Map([
[1, {
contractMapById: createIdMapFromList([
{
tenantId: 0,
id: 1,
version: 0,
Expand All @@ -66,18 +64,18 @@ const state: Partial<AppState> = {
maximumMinutesPerWeek: null,
maximumMinutesPerMonth: 10,
maximumMinutesPerYear: null,
}],
},
]),
},
skillList: {
isLoading: false,
skillMapById: new Map([
[3, {
skillMapById: createIdMapFromList([
{
tenantId: 0,
id: 3,
version: 0,
name: 'Skill 3',
}],
},
]),
},
};
Expand Down Expand Up @@ -276,19 +274,21 @@ describe('Employee reducers', () => {
expect(
reducer(state.employeeList, actions.addEmployee(addedEmployee)),
).toEqual({ ...state.employeeList,
employeeMapById: mapWithElement(storeState.employeeList.employeeMapById, addedEmployee) });
employeeMapById: storeState.employeeList.employeeMapById
.set(addedEmployee.id as number, mapDomainObjectToView(addedEmployee)) });
});
it('remove employee', () => {
expect(
reducer(state.employeeList, actions.removeEmployee(deletedEmployee)),
).toEqual({ ...state.employeeList,
employeeMapById: mapWithoutElement(storeState.employeeList.employeeMapById, deletedEmployee) });
employeeMapById: storeState.employeeList.employeeMapById.delete(deletedEmployee.id as number) });
});
it('update employee', () => {
expect(
reducer(state.employeeList, actions.updateEmployee(updatedEmployee)),
).toEqual({ ...state.employeeList,
employeeMapById: mapWithUpdatedElement(storeState.employeeList.employeeMapById, updatedEmployee) });
employeeMapById: storeState.employeeList.employeeMapById
.set(updatedEmployee.id as number, mapDomainObjectToView(updatedEmployee)) });
});
it('refresh employee list', () => {
expect(
Expand Down Expand Up @@ -351,22 +351,22 @@ describe('Employee selectors', () => {
...storeState,
skillList: { ...storeState.skillList, isLoading: true },
});
expect(employeeList).toEqual([]);
expect(employeeList).toEqual(List());
employeeList = employeeSelectors.getEmployeeList({
...storeState,
contractList: { ...storeState.contractList, isLoading: true },
});
expect(employeeList).toEqual([]);
expect(employeeList).toEqual(List());
employeeList = employeeSelectors.getEmployeeList({
...storeState,
employeeList: { ...storeState.employeeList, isLoading: true },
});
expect(employeeList).toEqual([]);
expect(employeeList).toEqual(List());
});

it('should return a list of all employee', () => {
const employeeList = employeeSelectors.getEmployeeList(storeState);
expect(employeeList).toEqual(expect.arrayContaining([
expect(employeeList.toArray()).toEqual(expect.arrayContaining([
{
tenantId: 0,
id: 1,
Expand Down Expand Up @@ -413,6 +413,6 @@ describe('Employee selectors', () => {
color: '#FFFFFF',
},
]));
expect(employeeList.length).toEqual(2);
expect(employeeList.size).toEqual(2);
});
});
Loading

0 comments on commit 5c97f69

Please sign in to comment.