{"version":3,"sources":["common/appContext/spec/sharedStateSpec.ts"],"names":[],"mappings":"","file":"../../../../common/appContext/spec/sharedStateSpec.d.ts","sourcesContent":["import { Barrier } from '../../mutexHelper.js';\nimport { AppContext, getTools } from '../appContext.js';\nimport { BusEvent, EventBus } from '../eventBus.js';\nimport { SharedState } from '../sharedState.js';\n\ninterface Tools {\n  bus: EventBus;\n  state: SharedState;\n}\n\ndescribe('the sharedState helper', () => {\n  let tools: Tools = null;\n\n  beforeAll((done) => {\n    AppContext.get().then((cx) => cx.onStart(\n      { bus: EventBus.providerName, state: SharedState.providerName },\n      async (toolBox) => {\n        tools = (await getTools(toolBox)) as Tools;\n        done();\n      },\n    ));\n  });\n\n  it('integrates with the app context', () => {\n    expect(tools).toBeDefined();\n    expect(tools).not.toBeNull();\n  });\n\n  const testKeyPrefix = 'lw-test/shardStateSpec';\n\n  it('initializes empty state', async () => {\n    const testKey = `${testKeyPrefix}/init`;\n    const state = await tools.state.getState(testKey);\n    expect(state).toEqual({});\n  });\n\n  it('triggers events on state change', async () => {\n    const testKey = `${testKeyPrefix}/change`;\n    const testValue = { whatever: 'bla bla', boris: 'turkey' };\n    const listenKey = `lw-sc:${testKey}`;\n    const doneBarrier = new Barrier<string>();\n    const listener = (ev: BusEvent) => {\n      expect(ev.data.old).toEqual({});\n      expect(ev.data.new).toEqual(testValue);\n      tools.bus.removeListener(listenKey, listener);\n      doneBarrier.signal('ok');\n    };\n\n    tools.bus.addListener(listenKey, listener);\n    const newValue = await tools.state.changeState(testKey, (value) => {\n      expect(value).toEqual({});\n      return testValue;\n    });\n    expect(newValue).toEqual(testValue);\n    await doneBarrier.wait();\n  });\n});\n"]}