From 6a4c772ad1529bd8c95e376e3837263af8c13771 Mon Sep 17 00:00:00 2001 From: Jason Wall Date: Sat, 15 Aug 2020 10:34:28 -0400 Subject: [PATCH] little reorg, added more unit tests to reducer, caught and fixed 1 bug --- .../app/services/app-state-reducer.spec.ts | 199 +++++++++++++----- app/db/src/app/services/app-state-reducer.ts | 25 ++- 2 files changed, 160 insertions(+), 64 deletions(-) diff --git a/app/db/src/app/services/app-state-reducer.spec.ts b/app/db/src/app/services/app-state-reducer.spec.ts index 8e836271..47e72bcb 100644 --- a/app/db/src/app/services/app-state-reducer.spec.ts +++ b/app/db/src/app/services/app-state-reducer.spec.ts @@ -12,10 +12,42 @@ describe('AppService Reducer', () => { user: null, cards: [], autocomplete: [], - currentSavedPage: null, + currentSavedPage: { + queries: [], + title: 'page1', + id: 'myid1', + }, savedPages: { createdOn: new Date(0).toISOString(), - value: [], + value: [ + { + queries: [ + { + qry: 'H1', + data: null, + type: CardType.Strongs, + } as CardItem, + ], + title: 'page1', + id: 'myid1', + }, + { + queries: [ + { + qry: 'H2', + data: null, + type: CardType.Strongs, + } as CardItem, + { + qry: 'G1', + data: null, + type: CardType.Strongs, + } as CardItem, + ], + title: 'page2', + id: 'myid2', + }, + ], }, notes: { createdOn: new Date(0).toISOString(), @@ -67,20 +99,7 @@ describe('AppService Reducer', () => { } }); - it('UPDATE_CARD_FONT_SIZE', () => { - const action = AppActionFactory.newUpdateCardFontSize(32); - const testState = reducer(preState, action); - expect(testState.displaySettings.value.cardFontSize).toBe(32, 'Failed to change card font size to 32'); - }); - - it('UPDATE_CARD_FONT_FAMILY', () => { - const action = AppActionFactory.newUpdateCardFontFamily('Jason'); - const testState = reducer(preState, action); - expect(testState.displaySettings.value.cardFontFamily).toBe( - 'Jason', - 'Failed to change card font family to "Jason"' - ); - }); + // UPDATE_PAGE_SETTINGS it('UPDATE_DISPLAY_SETTINGS', () => { const settings = { @@ -105,6 +124,107 @@ describe('AppService Reducer', () => { expect(testState.displaySettings).toBe(settings, 'Failed to update the display settings'); }); + it('UPDATE_CARD_FONT_SIZE', () => { + const action = AppActionFactory.newUpdateCardFontSize(32); + const testState = reducer(preState, action); + expect(testState.displaySettings.value.cardFontSize).toBe(32, 'Failed to change card font size to 32'); + }); + + it('UPDATE_CARD_FONT_FAMILY', () => { + const action = AppActionFactory.newUpdateCardFontFamily('Jason'); + const testState = reducer(preState, action); + expect(testState.displaySettings.value.cardFontFamily).toBe( + 'Jason', + 'Failed to change card font family to "Jason"' + ); + }); + + it('UPDATE_AUTOCOMPLETE', () => { + const words = ['word1', 'word2', 'word3']; + + const action = AppActionFactory.newUpdateAutocomplete(words); + const testState = reducer(preState, action); + expect(testState.autocomplete).toEqual(words, 'Failed to update the autocomplete array'); + }); + + it('UPDATE_SAVED_PAGES', () => { + const savedPages = new Storable([ + { + queries: [ + { + qry: 'H12', + data: null, + type: CardType.Strongs, + } as CardItem, + ], + title: 'testpage', + id: 'myid', + }, + ]); + + const action = AppActionFactory.newUpdateSavedPages(savedPages); + const testState = reducer(preState, action); + expect(testState.savedPages).toBe(savedPages, 'Failed to update the savedPages array'); + expect(testState.savedPages.value.length).toBe(1, 'Updated savedPages is the wrong length'); + expect(testState.savedPages.value[0].queries.length).toBe( + 1, + 'Updated savedPages first object has the wrong number of queries' + ); + }); + + it('UPDATE_SAVED_PAGE', () => { + const sp: SavedPage = { + queries: [], + title: 'test page', + id: 'myid2', + }; + + const action = AppActionFactory.newUpdateSavedPage(sp); + const testState = reducer(preState, action); + + expect(testState.savedPages.value[0].queries.length).toBe( + 1, + 'Updated savedPages first object has the wrong number of queries' + ); + expect(testState.savedPages.value[0].title).toBe('page1'); + expect(testState.savedPages.value[1].queries.length).toBe( + 0, + 'Updated savedPages first object has the wrong number of queries' + ); + expect(testState.savedPages.value[1].title).toBe('test page'); + }); + + it('REMOVE_SAVED_PAGE', () => { + const sp = preState.savedPages.value[0]; + const action = AppActionFactory.newRemoveSavedPage(sp); + const testState = reducer(preState, action); + + expect(testState.savedPages.value.length).toBe(1, 'Updated savedPages should be 1'); + expect(testState.savedPages.value[0].title).toBe('page2'); + }); + + it('UPDATE_CURRENT_PAGE', () => { + const card1: CardItem = { + qry: 'H123', + data: null, + type: CardType.Strongs, + }; + + const action1 = AppActionFactory.newAddCard(card1, null); + const testState = reducer(preState, action1); + expect(testState.cards[0]).toBe(card1, 'Failed to add first card to empty list'); + + const action = AppActionFactory.newUpdateCurrentPage(); + const testState2 = reducer(testState, action); + + expect(testState2.currentSavedPage.queries.length).toBe(1); + expect(preState.currentSavedPage.queries.length).toBe(0); + }); + + // 'SAVE_PAGE'; + // 'GET_SAVED_PAGE'; + // 'ADD_CARD_TO_SAVED_PAGE'; + it('ADD_CARD', () => { const card1: CardItem = { qry: 'H123', @@ -242,42 +362,13 @@ describe('AppService Reducer', () => { expect(testState6.cards[0]).toBe(card3, 'Failed to insert card at start of the list'); }); - it('UPDATE_AUTOCOMPLETE', () => { - const words = ['word1', 'word2', 'word3']; - - const action = AppActionFactory.newUpdateAutocomplete(words); - const testState = reducer(preState, action); - expect(testState.autocomplete).toEqual(words, 'Failed to update the autocomplete array'); - }); - - it('UPDATE_SAVED_PAGES', () => { - const savedPages = new Storable([ - { - queries: [ - { - qry: 'H123', - data: null, - type: CardType.Strongs, - } as CardItem, - { - qry: 'G123', - data: null, - type: CardType.Strongs, - } as CardItem, - ], - // tslint:disable-next-line: quotemark - title: "Jason's Page", - id: 'myid', - }, - ]); - - const action = AppActionFactory.newUpdateSavedPages(savedPages); - const testState = reducer(preState, action); - expect(testState.savedPages).toBe(savedPages, 'Failed to update the savedPages array'); - expect(testState.savedPages.value.length).toBe(1, 'Updated savedPages is the wrong length'); - expect(testState.savedPages.value[0].queries.length).toBe( - 2, - 'Updated savedPages first object has the wrong number of queries' - ); - }); + // 'UPDATE_CARD'; + // 'REMOVE_CARD'; + // 'MOVE_CARD'; + // 'SET_USER'; + // 'FIND_NOTES'; + // 'GET_NOTE'; + // 'UPDATE_NOTES'; + // 'SAVE_NOTE'; + // 'DELETE_NOTE'; }); diff --git a/app/db/src/app/services/app-state-reducer.ts b/app/db/src/app/services/app-state-reducer.ts index a63076bb..569ebdc1 100644 --- a/app/db/src/app/services/app-state-reducer.ts +++ b/app/db/src/app/services/app-state-reducer.ts @@ -44,7 +44,7 @@ export function reducer(state: AppState, action: AppAction): AppState { } switch (action.type) { - //#region + //#region Saved Page Settings case 'UPDATE_CARD_MERGE_STRATEGY': { const settings = new Storable({ @@ -136,7 +136,6 @@ export function reducer(state: AppState, action: AppAction): AppState { }; }); } - case 'REMOVE_SAVED_PAGE': { const savedPages = new Storable(state.savedPages.value.filter((o) => o.id !== action.savedPage.id)); @@ -149,18 +148,24 @@ export function reducer(state: AppState, action: AppAction): AppState { }); } case 'UPDATE_CURRENT_PAGE': { + const current = { + id: state.currentSavedPage.id, + title: state.currentSavedPage.title, + queries: [...mergeCardList(state.cards, state.pageSettings.value.mergeStrategy)], + }; + const savedPages = new Storable([ ...state.savedPages.value.filter((o) => o.id !== state.currentSavedPage.id), - { - id: state.currentSavedPage.id, - title: state.currentSavedPage.title, - queries: [...mergeCardList(state.cards, state.pageSettings.value.mergeStrategy)], - }, + current, ]); - return reducer(state, { - type: 'UPDATE_SAVED_PAGES', - savedPages, + return maybeMutateStorable(state, savedPages, state.savedPages, (item) => { + return { + ...state, + currentSavedPage: current, + savedPagesLoaded: true, + savedPages: item, + }; }); } case 'SAVE_PAGE': {