overleaf/services/web/test/frontend/shared/hooks/use-detach-state.test.js
Timothée Alby 44eca312ff Merge pull request #6053 from overleaf/ta-pdf-detach-tests
PDF Detach Misc Tests

GitOrigin-RevId: 9615c8fdfd8964a9c63d7c91e4596d397a1d35dc
2021-12-15 09:04:46 +00:00

68 lines
2 KiB
JavaScript

import { act } from '@testing-library/react-hooks'
import { expect } from 'chai'
import { renderHookWithEditorContext } from '../../helpers/render-with-context'
import sysendTestHelper from '../../helpers/sysend'
import useDetachState from '../../../../frontend/js/shared/hooks/use-detach-state'
const stateKey = 'some-key'
describe('useDetachState', function () {
beforeEach(function () {
window.metaAttributesCache = new Map()
})
afterEach(function () {
window.metaAttributesCache = new Map()
})
it('create and update state', async function () {
const defaultValue = 'foobar'
const { result } = renderHookWithEditorContext(() =>
useDetachState(stateKey, defaultValue)
)
const [value, setValue] = result.current
expect(value).to.equal(defaultValue)
expect(setValue).to.be.a('function')
const newValue = 'barbaz'
act(() => {
setValue(newValue)
})
expect(result.current[0]).to.equal(newValue)
})
it('broadcast message as sender', async function () {
window.metaAttributesCache.set('ol-detachRole', 'detacher')
const { result } = renderHookWithEditorContext(() =>
useDetachState(stateKey, null, 'detacher', 'detached')
)
const [, setValue] = result.current
sysendTestHelper.resetHistory()
const newValue = 'barbaz'
act(() => {
setValue(newValue)
})
expect(sysendTestHelper.getLastBroacastMessage()).to.deep.equal({
role: 'detacher',
event: `state-${stateKey}`,
data: { value: newValue },
})
})
it('receive message as target', async function () {
window.metaAttributesCache.set('ol-detachRole', 'detached')
const { result } = renderHookWithEditorContext(() =>
useDetachState(stateKey, null, 'detacher', 'detached')
)
const newValue = 'barbaz'
sysendTestHelper.receiveMessage({
role: 'detached',
event: `state-${stateKey}`,
data: { value: newValue },
})
expect(result.current[0]).to.equal(newValue)
})
})