mirror of
https://gitlab.com/walljm/dynamicbible.git
synced 2025-07-26 00:39:48 -04:00
add unit tests
This commit is contained in:
parent
4cbf9ad058
commit
2941c8ca6b
@ -6,6 +6,8 @@ import { Storable } from '../common/storable';
|
|||||||
import { CardType, CardItem } from '../models/card-state';
|
import { CardType, CardItem } from '../models/card-state';
|
||||||
import { SavedPage } from '../models/page-state';
|
import { SavedPage } from '../models/page-state';
|
||||||
import { AppState } from '../models/app-state';
|
import { AppState } from '../models/app-state';
|
||||||
|
import { getCardCacheKey } from '../common/card-cache-operations';
|
||||||
|
import { MoveDirection } from '../common/move-direction';
|
||||||
|
|
||||||
describe('getNewestStorable', () => {
|
describe('getNewestStorable', () => {
|
||||||
it('maybeMutateStorable', () => {
|
it('maybeMutateStorable', () => {
|
||||||
@ -448,9 +450,89 @@ describe('AppService Reducer', () => {
|
|||||||
expect(testState6.currentCards[0]).toBe(card3, 'Failed to insert card at start of the list');
|
expect(testState6.currentCards[0]).toBe(card3, 'Failed to insert card at start of the list');
|
||||||
});
|
});
|
||||||
|
|
||||||
// 'UPDATE_CARD';
|
it('UPDATE_CARD', () => {
|
||||||
// 'REMOVE_CARD';
|
const oldCard: CardItem = {
|
||||||
// 'MOVE_CARD';
|
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
|
//#endregion
|
||||||
|
|
||||||
|
@ -66,6 +66,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
|||||||
settings: item,
|
settings: item,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'UPDATE_CARD_FONT_SIZE': {
|
case 'UPDATE_CARD_FONT_SIZE': {
|
||||||
const settings = new Storable<Settings>({
|
const settings = new Storable<Settings>({
|
||||||
...state.settings.value,
|
...state.settings.value,
|
||||||
@ -80,6 +81,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
|||||||
settings,
|
settings,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'UPDATE_CARD_FONT_FAMILY': {
|
case 'UPDATE_CARD_FONT_FAMILY': {
|
||||||
const settings = new Storable<Settings>({
|
const settings = new Storable<Settings>({
|
||||||
...state.settings.value,
|
...state.settings.value,
|
||||||
@ -130,6 +132,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
|||||||
savedPages, // update the savedPages
|
savedPages, // update the savedPages
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'UPDATE_SAVED_PAGE': {
|
case 'UPDATE_SAVED_PAGE': {
|
||||||
const newSavedPages = new Storable<SavedPage[]>(
|
const newSavedPages = new Storable<SavedPage[]>(
|
||||||
state.savedPages.value.map((o) => {
|
state.savedPages.value.map((o) => {
|
||||||
@ -143,6 +146,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
|||||||
const savedPages = getNewestStorable(newSavedPages, state.savedPages);
|
const savedPages = getNewestStorable(newSavedPages, state.savedPages);
|
||||||
return reducer(state, AppActionFactory.newUpdateSavedPages(savedPages));
|
return reducer(state, AppActionFactory.newUpdateSavedPages(savedPages));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'REMOVE_SAVED_PAGE': {
|
case 'REMOVE_SAVED_PAGE': {
|
||||||
const savedPages = new Storable<SavedPage[]>(state.savedPages.value.filter((o) => o.id !== action.savedPage.id));
|
const savedPages = new Storable<SavedPage[]>(state.savedPages.value.filter((o) => o.id !== action.savedPage.id));
|
||||||
const item = getNewestStorable(savedPages, state.savedPages);
|
const item = getNewestStorable(savedPages, state.savedPages);
|
||||||
@ -153,6 +157,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
|||||||
savedPages: item,
|
savedPages: item,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'UPDATE_CURRENT_PAGE': {
|
case 'UPDATE_CURRENT_PAGE': {
|
||||||
const current = {
|
const current = {
|
||||||
id: state.currentSavedPage.id,
|
id: state.currentSavedPage.id,
|
||||||
@ -173,6 +178,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
|||||||
savedPages: item,
|
savedPages: item,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'SAVE_PAGE': {
|
case 'SAVE_PAGE': {
|
||||||
const savedPages = new Storable([
|
const savedPages = new Storable([
|
||||||
...(state.savedPages ? state.savedPages.value : []),
|
...(state.savedPages ? state.savedPages.value : []),
|
||||||
@ -189,6 +195,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
|||||||
savedPages,
|
savedPages,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'MOVE_SAVED_PAGE_CARD': {
|
case 'MOVE_SAVED_PAGE_CARD': {
|
||||||
const queries = moveItem(action.savedPage.queries, action.fromIndex, action.toIndex);
|
const queries = moveItem(action.savedPage.queries, action.fromIndex, action.toIndex);
|
||||||
const savedPage = {
|
const savedPage = {
|
||||||
@ -198,6 +205,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
|||||||
|
|
||||||
return reducer(state, AppActionFactory.newUpdateSavedPage(savedPage));
|
return reducer(state, AppActionFactory.newUpdateSavedPage(savedPage));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'ADD_CARD_TO_SAVED_PAGE': {
|
case 'ADD_CARD_TO_SAVED_PAGE': {
|
||||||
const savedPages = new Storable([
|
const savedPages = new Storable([
|
||||||
...(state.savedPages ? state.savedPages.value : []).map((o) => {
|
...(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),
|
cardCache: updateInCardCache(action.card, state.cardCache),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'UPDATE_CARD': {
|
case 'UPDATE_CARD': {
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@ -268,6 +277,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
|||||||
cardCache: updateInCardCache(action.newCard, removeFromCardCache(action.oldCard, state.cardCache)),
|
cardCache: updateInCardCache(action.newCard, removeFromCardCache(action.oldCard, state.cardCache)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'REMOVE_CARD': {
|
case 'REMOVE_CARD': {
|
||||||
// potentially remove card from a saved page.
|
// potentially remove card from a saved page.
|
||||||
const currentSavedPage =
|
const currentSavedPage =
|
||||||
|
Loading…
x
Reference in New Issue
Block a user