From 3533687a90dcaf3578fa182fddb04035d069640f Mon Sep 17 00:00:00 2001 From: Eric Bower Date: Sun, 19 Nov 2023 20:09:46 -0500 Subject: [PATCH] refactor: require data and loaders slices --- query/api.test.ts | 4 ++-- query/fetch.test.ts | 10 +++++----- query/mdw.test.ts | 6 +++--- store/query.ts | 6 +++--- store/schema.test.ts | 4 ++++ store/schema.ts | 9 ++++++++- 6 files changed, 25 insertions(+), 14 deletions(-) diff --git a/query/api.test.ts b/query/api.test.ts index 054132a..9d22e95 100644 --- a/query/api.test.ts +++ b/query/api.test.ts @@ -29,7 +29,7 @@ const testStore = () => { const schema = createSchema({ users: slice.table({ empty: emptyUser }), loaders: slice.loader(), - data: slice.table({ empty: {} }), + cache: slice.table({ empty: {} }), }); const store = configureStore(schema); return { schema, store }; @@ -298,7 +298,7 @@ it(tests, "createApi with hash key on a large post", async () => { }); expect([8, 9].includes(expectedKey.split("|")[1].length)).toBeTruthy(); - expect(s.data[expectedKey]).toEqual({ + expect(s.cache[expectedKey]).toEqual({ "1": { id: "1", name: "test", email: email, largetext: largetext }, }); }); diff --git a/query/fetch.test.ts b/query/fetch.test.ts index 8f64775..6e16922 100644 --- a/query/fetch.test.ts +++ b/query/fetch.test.ts @@ -23,7 +23,7 @@ const delay = (n = 200) => const testStore = () => { const schema = createSchema({ loaders: slice.loader(), - data: slice.table({ empty: {} }), + cache: slice.table({ empty: {} }), }); const store = configureStore(schema); return { schema, store }; @@ -68,7 +68,7 @@ it( await delay(); const state = store.getState(); - expect(state.data[action.payload.key]).toEqual(mockUser); + expect(state.cache[action.payload.key]).toEqual(mockUser); expect(actual).toEqual([{ url: `${baseUrl}/users`, @@ -151,7 +151,7 @@ it(tests, "error handling", async () => { await delay(); const state = store.getState(); - expect(state.data[action.payload.key]).toEqual(errMsg); + expect(state.cache[action.payload.key]).toEqual(errMsg); expect(actual).toEqual({ ok: false, data: errMsg, error: errMsg }); }); @@ -191,7 +191,7 @@ it(tests, "status 204", async () => { await delay(); const state = store.getState(); - expect(state.data[action.payload.key]).toEqual({}); + expect(state.cache[action.payload.key]).toEqual({}); expect(actual).toEqual({ ok: true, data: {}, value: {} }); }); @@ -429,7 +429,7 @@ it( await delay(); const state = store.getState(); - expect(state.data[action.payload.key]).toEqual(mockUser); + expect(state.cache[action.payload.key]).toEqual(mockUser); expect(actual).toEqual({ ok: true, data: mockUser, value: mockUser }); }, ); diff --git a/query/mdw.test.ts b/query/mdw.test.ts index e1ff212..440fc0b 100644 --- a/query/mdw.test.ts +++ b/query/mdw.test.ts @@ -34,7 +34,7 @@ const testStore = () => { const schema = createSchema({ users: slice.table({ empty: emptyUser }), loaders: slice.loader(), - data: slice.table({ empty: {} }), + cache: slice.table({ empty: {} }), }); const store = configureStore(schema); return { schema, store }; @@ -410,7 +410,7 @@ it(tests, "createApi with own key", async () => { : createKey("/users [POST]", { email: newUEmail }); const s = store.getState(); - asserts.assertEquals(schema.db.data.selectById(s, { id: expectedKey }), { + asserts.assertEquals(schema.db.cache.selectById(s, { id: expectedKey }), { "1": { id: "1", name: "test", email: newUEmail }, }); @@ -480,7 +480,7 @@ it(tests, "createApi with custom key but no payload", async () => { : createKey("/users [GET]", null); const s = store.getState(); - asserts.assertEquals(schema.db.data.selectById(s, { id: expectedKey }), { + asserts.assertEquals(schema.db.cache.selectById(s, { id: expectedKey }), { "1": mockUser, }); diff --git a/store/query.ts b/store/query.ts index b48237c..477cecf 100644 --- a/store/query.ts +++ b/store/query.ts @@ -9,15 +9,15 @@ import { TableOutput } from "./slice/table.ts"; export function storeMdw< Ctx extends ApiCtx = ApiCtx, M extends AnyState = AnyState, ->({ data, loaders, errorFn }: { +>({ cache, loaders, errorFn }: { loaders: LoaderOutput; - data: TableOutput; + cache: TableOutput; errorFn?: (ctx: Ctx) => string; }) { return compose([ dispatchActions, loadingMonitor(loaders, errorFn), - simpleCache(data), + simpleCache(cache), ]); } diff --git a/store/schema.test.ts b/store/schema.test.ts index 5337a29..3a49dee 100644 --- a/store/schema.test.ts +++ b/store/schema.test.ts @@ -25,6 +25,7 @@ it(tests, "general types and functionality", async () => { counter: slice.num(), dev: slice.any(false), currentUser: slice.obj(emptyUser), + cache: slice.table({ empty: {} }), loaders: slice.loader(), }); const db = schema.db; @@ -36,6 +37,7 @@ it(tests, "general types and functionality", async () => { counter: 0, dev: false, currentUser: { id: "", name: "" }, + cache: {}, loaders: {}, }); const userMap = schema.db.users.selectTable(store.getState()); @@ -74,6 +76,8 @@ it(tests, "general types and functionality", async () => { it(tests, "can work with a nested object", async () => { const schema = createSchema({ currentUser: slice.obj({ id: "", name: "", roles: [] }), + cache: slice.table({ empty: {} }), + loaders: slice.loader(), }); const db = schema.db; const store = configureStore(schema); diff --git a/store/schema.ts b/store/schema.ts index fb26dc9..e2744c0 100644 --- a/store/schema.ts +++ b/store/schema.ts @@ -1,8 +1,15 @@ +import { AnyState } from "../types.ts"; import { updateStore } from "./fx.ts"; +import { LoaderOutput } from "./slice/loader.ts"; +import { TableOutput } from "./slice/table.ts"; import { BaseSchema, FxStore, StoreUpdater } from "./types.ts"; export function createSchema< - O extends { [key: string]: (name: string) => BaseSchema }, + O extends { + loaders: (s: string) => LoaderOutput; + cache: (s: string) => TableOutput; + [key: string]: (name: string) => BaseSchema; + }, S extends { [key in keyof O]: ReturnType["initialState"] }, >( slices: O,