From 65d65b51fdb5b2e9d35e9d656991d2ccc76de16d Mon Sep 17 00:00:00 2001 From: Saul Date: Thu, 14 Mar 2024 13:35:58 +0100 Subject: [PATCH] fix: store instance in tests --- projects/ngrx-rtk-query/src/lib/create-api.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/projects/ngrx-rtk-query/src/lib/create-api.ts b/projects/ngrx-rtk-query/src/lib/create-api.ts index f123ce3..ba05664 100644 --- a/projects/ngrx-rtk-query/src/lib/create-api.ts +++ b/projects/ngrx-rtk-query/src/lib/create-api.ts @@ -39,17 +39,22 @@ export const createApi: CreateApi (api as unknown as Api, string, string, AngularHooksModule | CoreModule>).injector; + let store: Store; const getStore = () => { + if (store) return store; + const injector = getApiInjector(); if (!injector) { throw new Error( `Provide the API (${reducerPath}) is necessary to use the queries. Did you forget to provide the queries api?`, ); } - const store = injector.get(Store, undefined, { optional: true }); + const storeInstance = injector.get(Store, undefined, { optional: true }); if (!store) { throw new Error(`Provide the Store is necessary to use the queries. Did you forget to provide the store?`); } + + store = storeInstance as Store; return store; }; const storeDispatch = (action: Action) => { @@ -57,7 +62,7 @@ export const createApi: CreateApi { - const storeState = getStore().selectSignal((state) => state)(); + const storeState: Record = getStore().selectSignal((state) => state)(); return storeState?.[reducerPath] ? storeState : // Query inside forFeature (Code splitting)