mirror of
https://gitlab.com/walljm/dynamicbible.git
synced 2025-07-23 07:19:50 -04:00
unit tests for notes... fixed a couple of bugs from the cache work
This commit is contained in:
parent
2941c8ca6b
commit
5ea64d0cb9
@ -8,6 +8,7 @@ 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';
|
||||
import { NoteItem } from '../models/note-state';
|
||||
|
||||
describe('getNewestStorable', () => {
|
||||
it('maybeMutateStorable', () => {
|
||||
@ -540,11 +541,62 @@ describe('AppService Reducer', () => {
|
||||
|
||||
//#region Notes
|
||||
|
||||
// 'FIND_NOTES';
|
||||
// 'GET_NOTE';
|
||||
// 'UPDATE_NOTES';
|
||||
// 'SAVE_NOTE';
|
||||
// 'DELETE_NOTE';
|
||||
it('NOTES: Save, Get, ', () => {
|
||||
const note1: NoteItem = {
|
||||
id: '123456789',
|
||||
title: 'Test note 1',
|
||||
content: 'some content',
|
||||
xref: 'jn 3:16',
|
||||
};
|
||||
const note2: NoteItem = {
|
||||
id: '1234567890',
|
||||
title: 'Test note 2',
|
||||
content: 'some content',
|
||||
xref: 'jn 3:16',
|
||||
};
|
||||
|
||||
const action1 = AppActionFactory.newSaveNote(note1);
|
||||
const preState1 = reducer(preState, action1);
|
||||
const action2 = AppActionFactory.newSaveNote(note2);
|
||||
const preState2 = reducer(preState1, action2);
|
||||
expect(preState2.notes.value.length).toBe(2, 'Should have two notes');
|
||||
|
||||
const action3 = AppActionFactory.newGetNote('123456789', null);
|
||||
const preState3 = reducer(preState2, action3);
|
||||
expect(preState3.currentCards.length).toBe(1, 'Should have added the note card');
|
||||
|
||||
const action4 = AppActionFactory.newGetNote('1234567890', null);
|
||||
const preState4 = reducer(preState3, action4);
|
||||
expect(preState4.currentCards.length).toBe(2, 'Should have added the note card');
|
||||
});
|
||||
|
||||
it('NOTES: Update Notes, Delete Notes, Find Notes, ', () => {
|
||||
const note1: NoteItem = {
|
||||
id: '123456789',
|
||||
title: 'Test note 1',
|
||||
content: 'some content',
|
||||
xref: 'jn 3:16',
|
||||
};
|
||||
const note2: NoteItem = {
|
||||
id: '1234567890',
|
||||
title: 'Test note 2',
|
||||
content: 'some content',
|
||||
xref: 'jn 3:16',
|
||||
};
|
||||
|
||||
const action1 = AppActionFactory.newUpdateNotes(new Storable([note1, note2]));
|
||||
const preState1 = reducer(preState, action1);
|
||||
expect(preState1.notes.value.length).toBe(2, 'Should have added the notes');
|
||||
|
||||
const action2 = AppActionFactory.newFindNotes('note', null);
|
||||
const preState2 = reducer(preState1, action2);
|
||||
expect(preState2.currentCards.length).toBe(2, 'Should have found two notes card');
|
||||
|
||||
const action3 = AppActionFactory.newDeleteNote(note1);
|
||||
const preState3 = reducer(preState2, action3);
|
||||
expect(preState3.currentCards.length).toBe(1, 'Should have deleted the note card');
|
||||
expect(preState3.notes.value.length).toBe(1, 'Should have added the notes');
|
||||
});
|
||||
|
||||
//#endregion
|
||||
});
|
||||
|
@ -328,6 +328,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
||||
cardCache,
|
||||
};
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
case 'SET_USER': {
|
||||
@ -371,11 +372,19 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
||||
cards = [...notes, ...state.currentCards];
|
||||
}
|
||||
}
|
||||
|
||||
let cache = { ...state.cardCache };
|
||||
for (const n of notes) {
|
||||
cache = updateInCardCache(n, cache);
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
currentCards: cards,
|
||||
cardCache: cache,
|
||||
};
|
||||
}
|
||||
|
||||
case 'GET_NOTE': {
|
||||
const note = state.notes.value.find((o) => o.id === action.noteId);
|
||||
const card: CardItem = {
|
||||
@ -390,12 +399,14 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
||||
nextToItem: action.nextToItem,
|
||||
});
|
||||
}
|
||||
|
||||
case 'UPDATE_NOTES': {
|
||||
return {
|
||||
...state,
|
||||
notes: action.notes,
|
||||
};
|
||||
}
|
||||
|
||||
case 'SAVE_NOTE': {
|
||||
// you may be creating a new note or updating an existing.
|
||||
// if its an update, you need to update the note in the following:
|
||||
@ -405,49 +416,20 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
||||
// so iterate through all of them and if you find the note
|
||||
// in any of them, swap it out
|
||||
|
||||
const cards = [
|
||||
...state.currentCards.map((o) => {
|
||||
const n = getFromCardCache(o, state.cardCache).data as NoteItem;
|
||||
if (n && n.id === action.note.id) {
|
||||
return {
|
||||
...o,
|
||||
data: action.note,
|
||||
};
|
||||
}
|
||||
return o;
|
||||
}),
|
||||
];
|
||||
|
||||
const notes = new Storable<NoteItem[]>([
|
||||
...state.notes.value.filter((o) => o.id !== action.note.id),
|
||||
action.note,
|
||||
]);
|
||||
|
||||
const savedPages = new Storable<SavedPage[]>([
|
||||
...(state.savedPages ? state.savedPages.value : []).map((sp) => {
|
||||
return {
|
||||
...sp,
|
||||
queries: sp.queries.map((o) => {
|
||||
const n = getFromCardCache(o, state.cardCache).data as NoteItem;
|
||||
if (n && n.id === action.note.id) {
|
||||
return {
|
||||
...o,
|
||||
data: action.note,
|
||||
};
|
||||
}
|
||||
return o;
|
||||
}),
|
||||
};
|
||||
}),
|
||||
]);
|
||||
const newState = {
|
||||
...state,
|
||||
cards,
|
||||
currentCards: [...state.currentCards], // you want to trigger an update to the cards if a card update is different.
|
||||
notes,
|
||||
savedPages,
|
||||
};
|
||||
|
||||
return newState;
|
||||
}
|
||||
|
||||
case 'DELETE_NOTE': {
|
||||
// the note may be in any of the following:
|
||||
// * card list could have it.
|
||||
@ -456,10 +438,12 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
||||
// so iterate through all of them and if you find the note
|
||||
// in any of them, remove it
|
||||
|
||||
const card = state.currentCards.find((o) => o.qry === `note: ${action.note.id}`);
|
||||
|
||||
const cards = [
|
||||
...state.currentCards.filter((o) => {
|
||||
const n = getFromCardCache(o, state.cardCache).data as NoteItem;
|
||||
return !n || n.id !== action.note.id;
|
||||
const n = getFromCardCache(o, state.cardCache);
|
||||
return o.type !== CardType.Note || (n.data as NoteItem).id !== action.note.id;
|
||||
}),
|
||||
];
|
||||
|
||||
@ -470,17 +454,21 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
||||
return {
|
||||
...sp,
|
||||
queries: sp.queries.filter((o) => {
|
||||
const n = getFromCardCache(o, state.cardCache).data as NoteItem;
|
||||
return !n || n.id !== action.note.id;
|
||||
const n = getFromCardCache(o, state.cardCache);
|
||||
return o.type !== CardType.Note || (n.data as NoteItem).id !== action.note.id;
|
||||
}),
|
||||
};
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
...state,
|
||||
currentCards: cards,
|
||||
notes,
|
||||
savedPages,
|
||||
cardCache: card
|
||||
? removeFromCardCache(getFromCardCache(card, state.cardCache), state.cardCache)
|
||||
: state.cardCache,
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user