mirror of
https://gitlab.com/walljm/dynamicbible.git
synced 2025-07-25 08: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 { AppState } from '../models/app-state';
|
||||||
import { getCardCacheKey } from '../common/card-cache-operations';
|
import { getCardCacheKey } from '../common/card-cache-operations';
|
||||||
import { MoveDirection } from '../common/move-direction';
|
import { MoveDirection } from '../common/move-direction';
|
||||||
|
import { NoteItem } from '../models/note-state';
|
||||||
|
|
||||||
describe('getNewestStorable', () => {
|
describe('getNewestStorable', () => {
|
||||||
it('maybeMutateStorable', () => {
|
it('maybeMutateStorable', () => {
|
||||||
@ -540,11 +541,62 @@ describe('AppService Reducer', () => {
|
|||||||
|
|
||||||
//#region Notes
|
//#region Notes
|
||||||
|
|
||||||
// 'FIND_NOTES';
|
it('NOTES: Save, Get, ', () => {
|
||||||
// 'GET_NOTE';
|
const note1: NoteItem = {
|
||||||
// 'UPDATE_NOTES';
|
id: '123456789',
|
||||||
// 'SAVE_NOTE';
|
title: 'Test note 1',
|
||||||
// 'DELETE_NOTE';
|
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
|
//#endregion
|
||||||
});
|
});
|
||||||
|
@ -328,6 +328,7 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
|||||||
cardCache,
|
cardCache,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
case 'SET_USER': {
|
case 'SET_USER': {
|
||||||
@ -371,11 +372,19 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
|||||||
cards = [...notes, ...state.currentCards];
|
cards = [...notes, ...state.currentCards];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let cache = { ...state.cardCache };
|
||||||
|
for (const n of notes) {
|
||||||
|
cache = updateInCardCache(n, cache);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
currentCards: cards,
|
currentCards: cards,
|
||||||
|
cardCache: cache,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'GET_NOTE': {
|
case 'GET_NOTE': {
|
||||||
const note = state.notes.value.find((o) => o.id === action.noteId);
|
const note = state.notes.value.find((o) => o.id === action.noteId);
|
||||||
const card: CardItem = {
|
const card: CardItem = {
|
||||||
@ -390,12 +399,14 @@ export function reducer(state: AppState, action: AppAction): AppState {
|
|||||||
nextToItem: action.nextToItem,
|
nextToItem: action.nextToItem,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'UPDATE_NOTES': {
|
case 'UPDATE_NOTES': {
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
notes: action.notes,
|
notes: action.notes,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'SAVE_NOTE': {
|
case 'SAVE_NOTE': {
|
||||||
// you may be creating a new note or updating an existing.
|
// you may be creating a new note or updating an existing.
|
||||||
// if its an update, you need to update the note in the following:
|
// 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
|
// so iterate through all of them and if you find the note
|
||||||
// in any of them, swap it out
|
// 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[]>([
|
const notes = new Storable<NoteItem[]>([
|
||||||
...state.notes.value.filter((o) => o.id !== action.note.id),
|
...state.notes.value.filter((o) => o.id !== action.note.id),
|
||||||
action.note,
|
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 = {
|
const newState = {
|
||||||
...state,
|
...state,
|
||||||
cards,
|
currentCards: [...state.currentCards], // you want to trigger an update to the cards if a card update is different.
|
||||||
notes,
|
notes,
|
||||||
savedPages,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return newState;
|
return newState;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 'DELETE_NOTE': {
|
case 'DELETE_NOTE': {
|
||||||
// the note may be in any of the following:
|
// the note may be in any of the following:
|
||||||
// * card list could have it.
|
// * 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
|
// so iterate through all of them and if you find the note
|
||||||
// in any of them, remove it
|
// in any of them, remove it
|
||||||
|
|
||||||
|
const card = state.currentCards.find((o) => o.qry === `note: ${action.note.id}`);
|
||||||
|
|
||||||
const cards = [
|
const cards = [
|
||||||
...state.currentCards.filter((o) => {
|
...state.currentCards.filter((o) => {
|
||||||
const n = getFromCardCache(o, state.cardCache).data as NoteItem;
|
const n = getFromCardCache(o, state.cardCache);
|
||||||
return !n || n.id !== action.note.id;
|
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 {
|
return {
|
||||||
...sp,
|
...sp,
|
||||||
queries: sp.queries.filter((o) => {
|
queries: sp.queries.filter((o) => {
|
||||||
const n = getFromCardCache(o, state.cardCache).data as NoteItem;
|
const n = getFromCardCache(o, state.cardCache);
|
||||||
return !n || n.id !== action.note.id;
|
return o.type !== CardType.Note || (n.data as NoteItem).id !== action.note.id;
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
currentCards: cards,
|
currentCards: cards,
|
||||||
notes,
|
notes,
|
||||||
savedPages,
|
savedPages,
|
||||||
|
cardCache: card
|
||||||
|
? removeFromCardCache(getFromCardCache(card, state.cardCache), state.cardCache)
|
||||||
|
: state.cardCache,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user