Add an action to create entries and add some store tests.

master
Tom Hacohen 7 years ago
parent 441560dc72
commit 9c8df0a854

@ -0,0 +1,38 @@
import { entries, createEntries, fetchEntries } from './store';
import * as EteSync from './api/EteSync';
it('Entries reducer', () => {
const jId = '24324324324';
let state = {};
let entry = new EteSync.Entry();
entry.deserialize({
content: 'someContent',
uid: '6355209e2a2c26a6c1e6e967c2032737d538f602cf912474da83a2902f8a0a83'
});
let action = {
type: fetchEntries.toString(),
meta: {journal: jId, prevUid: null},
payload: [entry],
};
state = entries(state, action as any);
expect(state[jId].value[0].serialize()).toEqual(entry.serialize());
// We replace if there's no prevUid
state = entries(state, action as any);
expect(state[jId].value[0].serialize()).toEqual(entry.serialize());
expect(state[jId].value.length).toBe(1);
// We extend if prevUid is set
action.meta.prevUid = entry.uid;
state = entries(state, action as any);
expect(state[jId].value.length).toBe(2);
// Creating entries should also work the same
action.type = createEntries.toString();
state = entries(state, action as any);
expect(state[jId].value.length).toBe(3);
});

@ -92,7 +92,7 @@ export const { fetchCredentials, logout } = createActions({
LOGOUT: () => undefined, LOGOUT: () => undefined,
}); });
export const { fetchJournals, fetchEntries } = createActions({ export const { fetchJournals } = createActions({
FETCH_JOURNALS: (etesync: CredentialsData) => { FETCH_JOURNALS: (etesync: CredentialsData) => {
const creds = etesync.credentials; const creds = etesync.credentials;
const apiBase = etesync.serviceApiUrl; const apiBase = etesync.serviceApiUrl;
@ -100,17 +100,32 @@ export const { fetchJournals, fetchEntries } = createActions({
return journalManager.list(); return journalManager.list();
}, },
});
export const { fetchEntries, createEntries } = createActions({
FETCH_ENTRIES: [ FETCH_ENTRIES: [
(etesync: CredentialsData, journalUid: string, prevUid: string | null) => { (etesync: CredentialsData, journalUid: string, prevUid: string | null) => {
const creds = etesync.credentials; const creds = etesync.credentials;
const apiBase = etesync.serviceApiUrl; const apiBase = etesync.serviceApiUrl;
let entryManager = new EteSync.EntryManager(creds, apiBase, journalUid); let entryManager = new EteSync.EntryManager(creds, apiBase, journalUid);
return entryManager.list(prevUid) as any; return entryManager.list(prevUid);
}, },
(etesync: CredentialsData, journalUid: string, prevUid: string | null) => { (etesync: CredentialsData, journalUid: string, prevUid: string | null) => {
return { journal: journalUid, prevUid }; return { journal: journalUid, prevUid };
} }
],
CREATE_ENTRIES: [
(etesync: CredentialsData, journalUid: string, newEntries: Array<EteSync.Entry>, prevUid: string | null) => {
const creds = etesync.credentials;
const apiBase = etesync.serviceApiUrl;
let entryManager = new EteSync.EntryManager(creds, apiBase, journalUid);
return entryManager.create(newEntries, prevUid).then(response => newEntries);
},
(etesync: CredentialsData, journalUid: string, newEntries: Array<EteSync.Entry>, prevUid: string | null) => {
return { journal: journalUid, entries: newEntries, prevUid };
}
] ]
}); });
@ -124,8 +139,8 @@ const credentials = handleActions(
{value: null} {value: null}
); );
const entries = handleAction( export const entries = handleAction(
fetchEntries, combineActions(fetchEntries, createEntries),
(state: EntriesType, action: any) => { (state: EntriesType, action: any) => {
const prevState = state[action.meta.journal]; const prevState = state[action.meta.journal];
const extend = action.meta.prevUid != null; const extend = action.meta.prevUid != null;

Loading…
Cancel
Save