add unit tests

This commit is contained in:
Jason Wall 2020-08-23 09:18:09 -04:00
parent 4cbf9ad058
commit 2941c8ca6b
2 changed files with 95 additions and 3 deletions

View File

@ -6,6 +6,8 @@ import { Storable } from '../common/storable';
import { CardType, CardItem } from '../models/card-state';
import { SavedPage } from '../models/page-state';
import { AppState } from '../models/app-state';
import { getCardCacheKey } from '../common/card-cache-operations';
import { MoveDirection } from '../common/move-direction';
describe('getNewestStorable', () => {
it('maybeMutateStorable', () => {
@ -448,9 +450,89 @@ describe('AppService Reducer', () => {
expect(testState6.currentCards[0]).toBe(card3, 'Failed to insert card at start of the list');
});
// 'UPDATE_CARD';
// 'REMOVE_CARD';
// 'MOVE_CARD';
it('UPDATE_CARD', () => {
const oldCard: CardItem = {
qry: 'G123',
data: null,
type: CardType.Strongs,
};
const action1 = AppActionFactory.newAddCard(oldCard, null);
const preState1 = reducer(preState, action1);
const newCard: CardItem = {
qry: 'H88',
data: null,
type: CardType.Strongs,
};
const action2 = AppActionFactory.newUpdateCard(newCard, oldCard);
const testState = reducer(preState1, action2);
expect(testState.currentCards[0].qry).toBe('H88', 'Should update the card');
expect(testState.cardCache[getCardCacheKey(newCard)].qry).toBe('H88', 'Should exist in card cache');
expect(testState.cardCache[getCardCacheKey(oldCard)]).toBeUndefined(
'Should be undefined, having been removed from card cache'
);
});
it('REMOVE_CARD', () => {
const card: CardItem = {
qry: 'G123',
data: null,
type: CardType.Strongs,
};
const action1 = AppActionFactory.newAddCard(card, null);
const preState1 = reducer(preState, action1);
const action2 = AppActionFactory.newRemoveCard(card);
const testState = reducer(preState1, action2);
expect(preState1.currentCards.length).toBe(1, 'Should have added the card in preparation for removing the card');
expect(testState.currentCards.length).toBe(0, 'Should have removed the card');
expect(testState.cardCache[getCardCacheKey(card)]).toBeUndefined(
'Should be undefined, having been removed from card cache'
);
});
it('MOVE_CARD', () => {
const card1: CardItem = {
qry: 'G123',
data: null,
type: CardType.Strongs,
};
const action1 = AppActionFactory.newAddCard(card1, null);
const preState1 = reducer(preState, action1);
const card2: CardItem = {
qry: 'H88',
data: null,
type: CardType.Strongs,
};
const action2 = AppActionFactory.newAddCard(card2, null);
const preState2 = reducer(preState1, action2);
expect(preState2.currentCards.length).toBe(2, 'Should have two cards');
expect(preState2.currentCards[0].qry).toBe('G123');
expect(preState2.currentCards[1].qry).toBe('H88');
const action3 = AppActionFactory.newMoveCard(card2, MoveDirection.Up);
const testState1 = reducer(preState2, action3);
expect(testState1.currentCards[0].qry).toBe('H88');
expect(testState1.currentCards[1].qry).toBe('G123');
const testState4 = reducer(preState2, action3);
expect(testState4.currentCards[0].qry).toBe('H88');
expect(testState4.currentCards[1].qry).toBe('G123');
const action4 = AppActionFactory.newMoveCard(card1, MoveDirection.Down);
const testState2 = reducer(preState2, action4);
expect(testState2.currentCards[0].qry).toBe('H88');
expect(testState2.currentCards[1].qry).toBe('G123');
const testState3 = reducer(preState2, action4);
expect(testState3.currentCards[0].qry).toBe('H88');
expect(testState3.currentCards[1].qry).toBe('G123');
});
//#endregion

View File

@ -66,6 +66,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
settings: item,
};
}
case 'UPDATE_CARD_FONT_SIZE': {
const settings = new Storable<Settings>({
...state.settings.value,
@ -80,6 +81,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
settings,
});
}
case 'UPDATE_CARD_FONT_FAMILY': {
const settings = new Storable<Settings>({
...state.settings.value,
@ -130,6 +132,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
savedPages, // update the savedPages
};
}
case 'UPDATE_SAVED_PAGE': {
const newSavedPages = new Storable<SavedPage[]>(
state.savedPages.value.map((o) => {
@ -143,6 +146,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
const savedPages = getNewestStorable(newSavedPages, state.savedPages);
return reducer(state, AppActionFactory.newUpdateSavedPages(savedPages));
}
case 'REMOVE_SAVED_PAGE': {
const savedPages = new Storable<SavedPage[]>(state.savedPages.value.filter((o) => o.id !== action.savedPage.id));
const item = getNewestStorable(savedPages, state.savedPages);
@ -153,6 +157,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
savedPages: item,
};
}
case 'UPDATE_CURRENT_PAGE': {
const current = {
id: state.currentSavedPage.id,
@ -173,6 +178,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
savedPages: item,
};
}
case 'SAVE_PAGE': {
const savedPages = new Storable([
...(state.savedPages ? state.savedPages.value : []),
@ -189,6 +195,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
savedPages,
});
}
case 'MOVE_SAVED_PAGE_CARD': {
const queries = moveItem(action.savedPage.queries, action.fromIndex, action.toIndex);
const savedPage = {
@ -198,6 +205,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
return reducer(state, AppActionFactory.newUpdateSavedPage(savedPage));
}
case 'ADD_CARD_TO_SAVED_PAGE': {
const savedPages = new Storable([
...(state.savedPages ? state.savedPages.value : []).map((o) => {
@ -256,6 +264,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
cardCache: updateInCardCache(action.card, state.cardCache),
};
}
case 'UPDATE_CARD': {
return {
...state,
@ -268,6 +277,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
cardCache: updateInCardCache(action.newCard, removeFromCardCache(action.oldCard, state.cardCache)),
};
}
case 'REMOVE_CARD': {
// potentially remove card from a saved page.
const currentSavedPage =