diff --git a/components/command-react/package.json b/components/command-react/package.json index 84ba11f6f..3c2cbc527 100644 --- a/components/command-react/package.json +++ b/components/command-react/package.json @@ -16,7 +16,7 @@ "lint": "eslint --ext=ts,tsx,js,yaml,yml .", "start": "react-scripts start", "storybook": "start-storybook -p 6006 -s public", - "test": "react-app-rewired test --watchAll=false", + "test": "react-app-rewired test --watchAll=false 2>&1", "test:visual": "cross-env VISUAL_TEST=1 react-app-rewired test --watchAll=false", "test:watch": "react-app-rewired test", "verify": "run-p build lint depcheck test", diff --git a/components/command-react/src/__snapshots__/index.spec.ts.snap b/components/command-react/src/__snapshots__/index.spec.ts.snap new file mode 100644 index 000000000..2e6c35375 --- /dev/null +++ b/components/command-react/src/__snapshots__/index.spec.ts.snap @@ -0,0 +1,561 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Storyshots Command Palette/Command Palette No Command 1`] = ` +Array [ +
+ ctrl+p to show the command palette +
, +
+ +
+
+
+
+
+ +
+
+
+
+
+
, +] +`; + +exports[`Storyshots Command Palette/Command Palette One Command 1`] = ` +Array [ +
+ ctrl+p to show the command palette +
, +
+ +
+
+
+
+
+ +
+
+
+
+
+
, +] +`; + +exports[`Storyshots Command Palette/Command Palette Override Mac Command In Mac 1`] = ` +Array [ +
+ ctrl+p to show the command palette +
, +
+ +
+
+
+
+
+ +
+
+
+
+
+
, +] +`; + +exports[`Storyshots Command Palette/Command Palette Override Mac Command In Window 1`] = ` +Array [ +
+ ctrl+p to show the command palette +
, +
+ +
+
+
+
+
+ +
+
+
+
+
+
, +] +`; + +exports[`Storyshots Command Palette/Command Palette With Key 1`] = ` +Array [ +
+ ctrl+p to show the command palette +
, +
+ +
+
+
+
+
+ +
+
+
+
+
+
, +] +`; diff --git a/frameworks/app/package.json b/frameworks/app/package.json index c36707a71..5a33e700c 100644 --- a/frameworks/app/package.json +++ b/frameworks/app/package.json @@ -10,7 +10,7 @@ "coverage": "jest --coverage", "depcheck": "depcheck", "lint": "eslint --ext=ts,tsx,js,yaml,yml .", - "test": "jest", + "test": "jest 2>&1", "test:watch": "jest --watch", "verify": "run-p build lint depcheck test", "verify:build": "run-s clean build" diff --git a/frameworks/commands/package.json b/frameworks/commands/package.json index d4facaeba..71084931e 100644 --- a/frameworks/commands/package.json +++ b/frameworks/commands/package.json @@ -10,7 +10,7 @@ "coverage": "jest --coverage", "depcheck": "depcheck", "lint": "eslint --ext=ts,tsx,js,yaml,yml .", - "test": "jest", + "test": "jest 2>&1", "test:watch": "jest --watch", "verify": "run-p build lint depcheck test", "verify:build": "run-s clean build" diff --git a/frameworks/commands/ts/activate.ts b/frameworks/commands/ts/activate.ts index 62c81dfa8..9ab0e6752 100644 --- a/frameworks/commands/ts/activate.ts +++ b/frameworks/commands/ts/activate.ts @@ -7,9 +7,7 @@ import { Command } from './types' */ export function activate() { // TODO: handle when user uses ES5 only and does not support `Map` - const [commands, setCommands, onChange] = createState>( - store.getCommands() - ) + const [commands, setCommands, onChange] = createState>(store.get()) store.activate(commands, setCommands, onChange) return { onChange } diff --git a/frameworks/commands/ts/commands.ts b/frameworks/commands/ts/commands.ts index c3cdc8db6..71d891502 100644 --- a/frameworks/commands/ts/commands.ts +++ b/frameworks/commands/ts/commands.ts @@ -7,30 +7,30 @@ import { Command } from './types' export type CommandRegistration = Omit export function getCommands() { - return store.getCommands() + return store.get() } export function registerCommand(id: string, command: CommandRegistration) { log.trace('registerCommand', id) - const commands = store.getCommands() + const commands = store.get() if (commands.has(id)) { log.warn(`Registering an already registered command: '${id}'`) return } const cmd = { ...command, id } - store.setCommands(produce(commands, m => m.set(id, cmd))) + store.set(produce(commands, m => m.set(id, cmd))) } export function invokeCommand(id: string) { log.trace('invokeCommand', id) - const cmd = store.getCommands().get(id) + const cmd = store.get().get(id) cmd ? cmd.handler() : log.error(`Invoking not registered command: '${id}'`) } export function clearCommands() { log.trace('clearCommands') - store.setCommands(produce( - store.getCommands(), + store.set(produce( + store.get(), (m: Map) => void m.clear())) } diff --git a/frameworks/commands/ts/store.ts b/frameworks/commands/ts/store.ts index 9a2bddc09..965a6e08b 100644 --- a/frameworks/commands/ts/store.ts +++ b/frameworks/commands/ts/store.ts @@ -1,17 +1,4 @@ -import { OnStateChange, SetState } from '@just-web/states' +import { createStore } from '@just-web/states' import { Command } from './types' -let cmds: Map = new Map() -let setCmds: SetState> = (value) => cmds = value -export const store = { - getCommands() { return cmds }, - setCommands(commands: Map) { setCmds(commands) }, - activate( - commands: Map, - setValue: SetState>, - onChange: OnStateChange>) { - cmds = commands - onChange(v => cmds = v) - setCmds = setValue - } -} +export const store = createStore(new Map()) diff --git a/frameworks/errors/package.json b/frameworks/errors/package.json index cae42096c..74405cb71 100644 --- a/frameworks/errors/package.json +++ b/frameworks/errors/package.json @@ -10,7 +10,7 @@ "coverage": "jest --coverage", "depcheck": "depcheck", "lint": "eslint --ext=ts,tsx,js,yaml,yml .", - "test": "jest", + "test": "jest 2>&1", "test:watch": "jest --watch", "verify": "run-p build lint depcheck test", "verify:build": "run-s clean build" diff --git a/frameworks/errors/ts/errorStore.ts b/frameworks/errors/ts/errorStore.ts new file mode 100644 index 000000000..63c6b23d2 --- /dev/null +++ b/frameworks/errors/ts/errorStore.ts @@ -0,0 +1,4 @@ +import { createStore } from '@just-web/states' +import { ModuleError } from 'iso-error' + +export const errorStore = createStore([]) diff --git a/frameworks/errors/ts/errors.ts b/frameworks/errors/ts/errors.ts index 80d7d095f..cba2456e4 100644 --- a/frameworks/errors/ts/errors.ts +++ b/frameworks/errors/ts/errors.ts @@ -13,6 +13,5 @@ export class BrowserError extends JustWebError { public colno?: number, error?: Error) { super(event.toString(), { cause: error }) - } } diff --git a/frameworks/errors/ts/onerror.spec.ts b/frameworks/errors/ts/onerror.spec.ts index 4ff84002c..db66ea573 100644 --- a/frameworks/errors/ts/onerror.spec.ts +++ b/frameworks/errors/ts/onerror.spec.ts @@ -4,7 +4,7 @@ import { stub } from 'type-plus' import { registerOnErrorHandler } from './onerror' test('capture error', () => { - const [errors, setErrors] = createState([]) + const [errors, setErrors, onChange] = createState([]) const ctx: registerOnErrorHandler.Ctx = { window: {} } as any registerOnErrorHandler({ @@ -13,8 +13,11 @@ test('capture error', () => { preventDefault: false }, stub(ctx)) + let actual: ModuleError[] + + onChange(v => actual = v) ctx.window.onerror!('some error occurred') - expect(errors.length).toBe(1) - expect(errors[0].message).toBe('some error occurred') + expect(actual!.length).toBe(1) + expect(actual![0].message).toBe('some error occurred') }) diff --git a/frameworks/errors/ts/store.ts b/frameworks/errors/ts/optionsStore.ts similarity index 100% rename from frameworks/errors/ts/store.ts rename to frameworks/errors/ts/optionsStore.ts diff --git a/frameworks/errors/ts/start.spec.ts b/frameworks/errors/ts/start.spec.ts index b769106b7..960f5d88f 100644 --- a/frameworks/errors/ts/start.spec.ts +++ b/frameworks/errors/ts/start.spec.ts @@ -1,9 +1,8 @@ import { start } from './start' -import { getStore } from './store' +import { getStore } from './optionsStore' test('by default browser error prevent default is true', async () => { await start() - const store = getStore() expect(store.browserErrors.preventDefault).toBe(true) }) diff --git a/frameworks/errors/ts/start.ts b/frameworks/errors/ts/start.ts index cc9f76a79..4e9874156 100644 --- a/frameworks/errors/ts/start.ts +++ b/frameworks/errors/ts/start.ts @@ -1,20 +1,24 @@ import { createState } from '@just-web/states' import type { ModuleError } from 'iso-error' import { RecursivePartial, requiredDeep } from 'type-plus' +import { errorStore } from './errorStore' import { registerOnErrorHandler } from './onerror' -import { getStore, setStore, Store } from './store' +import { getStore, setStore, Store } from './optionsStore' export namespace start { export type Options = Store } export async function start(options?: RecursivePartial) { - const [errors, setErrors] = createState([]) + const [errors, setErrors, onChange] = createState(errorStore.get()) + + errorStore.activate(errors, setErrors, onChange) + + const optionStore = setStore(requiredDeep(getStore(), options)) - const store = setStore(requiredDeep(getStore(), options)) registerOnErrorHandler({ errors, setErrors, - preventDefault: store.browserErrors.preventDefault + preventDefault: optionStore.browserErrors.preventDefault }) } diff --git a/frameworks/platform/package.json b/frameworks/platform/package.json index ac5e134ba..112139df4 100644 --- a/frameworks/platform/package.json +++ b/frameworks/platform/package.json @@ -10,7 +10,7 @@ "coverage": "jest --coverage", "depcheck": "depcheck", "lint": "eslint --ext=ts,tsx,js,yaml,yml .", - "test": "jest", + "test": "jest 2>&1", "test:watch": "jest --watch", "verify": "run-p build lint depcheck test", "verify:build": "run-s clean build" diff --git a/frameworks/routes/package.json b/frameworks/routes/package.json index 95a68f34f..2dc081de2 100644 --- a/frameworks/routes/package.json +++ b/frameworks/routes/package.json @@ -10,7 +10,7 @@ "coverage": "jest --coverage", "depcheck": "depcheck", "lint": "eslint --ext=ts,tsx,js,yaml,yml .", - "test": "jest", + "test": "jest 2>&1", "test:watch": "jest --watch", "verify": "run-p build lint depcheck test", "verify:build": "run-s clean build" diff --git a/frameworks/states/package.json b/frameworks/states/package.json index 74de5d180..ed02e215e 100644 --- a/frameworks/states/package.json +++ b/frameworks/states/package.json @@ -10,7 +10,7 @@ "coverage": "jest --coverage", "depcheck": "depcheck", "lint": "eslint --ext=ts,tsx,js,yaml,yml .", - "test": "jest", + "test": "jest 2>&1", "test:watch": "jest --watch", "verify": "run-p build lint depcheck test", "verify:build": "run-s clean build" diff --git a/frameworks/states/ts/index.ts b/frameworks/states/ts/index.ts index e10018498..8c0b9f1b5 100644 --- a/frameworks/states/ts/index.ts +++ b/frameworks/states/ts/index.ts @@ -1,2 +1,3 @@ export { enableAllPlugins, enableES5, enableMapSet, enablePatches } from 'immer' export * from './state' +export * from './store' diff --git a/frameworks/states/ts/store.spec.ts b/frameworks/states/ts/store.spec.ts new file mode 100644 index 000000000..9accd37c7 --- /dev/null +++ b/frameworks/states/ts/store.spec.ts @@ -0,0 +1,40 @@ +import produce, { enableMapSet } from 'immer' +import { assertType } from 'type-plus' +import { createState } from './state' +import { createStore } from './store' + +describe('createStore()', () => { + test('get() returns initial value', () => { + const value = new Map() + value.set('x', { a: 1 }) + const store = createStore(value) + const a = store.get() + expect(a).toStrictEqual(value) + }) + test('set() value and get by get()', () => { + const original = new Map() + const store = createStore(original) + const value = new Map() + value.set('b', { a: 2 }) + store.set(value) + const a = store.get() + expect(a).toStrictEqual(value) + }) + + test('set() triggers onChange()', () => { + enableMapSet() + const original = new Map() + const store = createStore(original) + + const [value, setValue, onChange] = createState(original) + let actual: Map + onChange(v => actual = v) + store.activate(value, setValue, onChange) + + setValue(produce(value, v => v.set('c', { a: 3 }))) + assertType>(actual!) + + expect(actual.size).toStrictEqual(1) + expect(actual.get('c')).toEqual({ a: 3 }) + }) +}) diff --git a/frameworks/states/ts/store.ts b/frameworks/states/ts/store.ts new file mode 100644 index 000000000..8f0cccb54 --- /dev/null +++ b/frameworks/states/ts/store.ts @@ -0,0 +1,17 @@ +import { OnStateChange, SetState } from './state' + +export function createStore(value: T) { + let setter: SetState = v => value = v + return { + get() { return value }, + set(value: T) { setter(value) }, + activate( + v: T, + setValue: SetState, + onChange: OnStateChange) { + value = v + onChange(v => value = v) + setter = setValue + } + } +} diff --git a/just-func/play-react/package.json b/just-func/play-react/package.json index 34aecd503..f11a1b2d8 100644 --- a/just-func/play-react/package.json +++ b/just-func/play-react/package.json @@ -13,7 +13,7 @@ "lint": "eslint --ext=ts,tsx,js,yaml,yml .", "start": "react-scripts start", "storybook": "start-storybook -p 6006 -s public", - "test": "react-app-rewired test --watchAll=false", + "test": "react-app-rewired test --watchAll=false 2>&1", "test:visual": "cross-env VISUAL_TEST=1 react-app-rewired test --watchAll=false", "test:watch": "react-app-rewired test", "verify": "run-p build lint depcheck test", diff --git a/just-func/play-solid/package.json b/just-func/play-solid/package.json index 7888a2f5a..e97b9a60c 100644 --- a/just-func/play-solid/package.json +++ b/just-func/play-solid/package.json @@ -14,7 +14,7 @@ "serve": "vite preview", "start": "vite", "storybook": "start-storybook -p 6006", - "test": "jest", + "test": "jest 2>&1", "test:watch": "jest --watch", "verify": "run-p build lint depcheck test", "verify:build": "run-s clean build" diff --git a/tools/testing/package.json b/tools/testing/package.json index 25e4bc8c4..5aa1956f9 100644 --- a/tools/testing/package.json +++ b/tools/testing/package.json @@ -10,7 +10,7 @@ "coverage": "jest --coverage", "depcheck": "depcheck", "lint": "eslint --ext=ts,tsx,js,yaml,yml .", - "test": "jest", + "test": "jest 2>&1", "test:watch": "jest --watch", "verify": "run-p build lint depcheck test", "verify:build": "run-s clean build"