-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
495 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 21 additions & 52 deletions
73
packages/esm-implementer-tools-app/__mocks__/openmrs-esm-api.mock.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,28 @@ | ||
import createStore, { Store } from "unistore"; | ||
|
||
export function openmrsFetch() { | ||
return new Promise(() => {}); | ||
} | ||
|
||
interface StoreEntity { | ||
value: Store<any>; | ||
active: boolean; | ||
} | ||
|
||
const availableStores: Record<string, StoreEntity> = {}; | ||
|
||
export function createGlobalStore<TState>( | ||
name: string, | ||
initialState: TState | ||
): Store<TState> { | ||
const available = availableStores[name]; | ||
|
||
if (available) { | ||
if (available.active) { | ||
console.error( | ||
"Cannot override an existing store. Make sure that stores are only created once." | ||
); | ||
} else { | ||
available.value.setState(initialState, true); | ||
} | ||
|
||
available.active = true; | ||
return available.value; | ||
} else { | ||
const store = createStore(initialState); | ||
|
||
availableStores[name] = { | ||
value: store, | ||
active: true, | ||
}; | ||
|
||
return store; | ||
} | ||
let state; | ||
|
||
function makeStore(state) { | ||
return { | ||
getState: () => state, | ||
setState: (val) => { | ||
state = { ...state, ...val }; | ||
}, | ||
subscribe: (updateFcn) => { | ||
updateFcn(state); | ||
return () => {}; | ||
}, | ||
unsubscribe: () => {}, | ||
}; | ||
} | ||
|
||
export function getGlobalStore<TState = any>( | ||
name: string, | ||
fallbackState?: TState | ||
): Store<TState> { | ||
const available = availableStores[name]; | ||
export const createGlobalStore = jest.fn().mockImplementation((n, value) => { | ||
state = value; | ||
return makeStore(state); | ||
}); | ||
|
||
if (!available) { | ||
const store = createStore(fallbackState); | ||
availableStores[name] = { | ||
value: store, | ||
active: false, | ||
}; | ||
return store; | ||
} | ||
|
||
return available.value; | ||
} | ||
export const getGlobalStore = jest | ||
.fn() | ||
.mockImplementation(() => makeStore(state)); |
2 changes: 0 additions & 2 deletions
2
packages/esm-implementer-tools-app/__mocks__/openmrs-esm-extensions.mock.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.