2024-07-24 09:40:24 -04:00
|
|
|
import '../../helpers/bootstrap-3'
|
2023-01-09 07:52:11 -05:00
|
|
|
import { FC } from 'react'
|
|
|
|
import useDetachState from '../../../../frontend/js/shared/hooks/use-detach-state'
|
|
|
|
import { EditorProviders } from '../../helpers/editor-providers'
|
|
|
|
import { detachChannel, testDetachChannel } from '../../helpers/detach-channel'
|
|
|
|
|
|
|
|
const DetachStateTest: FC<{
|
|
|
|
stateKey: string
|
|
|
|
defaultValue: any
|
|
|
|
senderRole?: string
|
|
|
|
targetRole?: string
|
|
|
|
handleClick: (setValue: (value: any) => void) => void
|
|
|
|
}> = ({ stateKey, defaultValue, handleClick, senderRole, targetRole }) => {
|
|
|
|
const [value, setValue] = useDetachState(
|
|
|
|
stateKey,
|
|
|
|
defaultValue,
|
|
|
|
senderRole,
|
|
|
|
targetRole
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div id="value">{value}</div>
|
|
|
|
<button id="setValue" onClick={() => handleClick(setValue)}>
|
|
|
|
set value
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('useDetachState', function () {
|
|
|
|
it('create and update state', function () {
|
|
|
|
cy.mount(
|
|
|
|
<EditorProviders>
|
|
|
|
<DetachStateTest
|
|
|
|
stateKey="some-key"
|
|
|
|
defaultValue="foobar"
|
|
|
|
handleClick={setValue => {
|
|
|
|
setValue('barbaz')
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</EditorProviders>
|
|
|
|
)
|
|
|
|
|
|
|
|
cy.get('#value').should('have.text', 'foobar')
|
|
|
|
cy.get('#setValue').click()
|
|
|
|
cy.get('#value').should('have.text', 'barbaz')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('broadcast message as sender', function () {
|
|
|
|
window.metaAttributesCache.set('ol-detachRole', 'detacher')
|
|
|
|
|
|
|
|
cy.mount(
|
|
|
|
<EditorProviders>
|
|
|
|
<DetachStateTest
|
|
|
|
stateKey="some-key"
|
|
|
|
defaultValue={null}
|
|
|
|
senderRole="detacher"
|
|
|
|
targetRole="detached"
|
|
|
|
handleClick={setValue => {
|
|
|
|
setValue('barbaz1')
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</EditorProviders>
|
|
|
|
)
|
|
|
|
|
|
|
|
cy.spy(detachChannel, 'postMessage').as('postDetachMessage')
|
|
|
|
cy.get('#setValue').click()
|
2023-05-24 06:03:36 -04:00
|
|
|
cy.get('@postDetachMessage').should('have.been.calledWith', {
|
2023-01-09 07:52:11 -05:00
|
|
|
role: 'detacher',
|
|
|
|
event: 'state-some-key',
|
|
|
|
data: { value: 'barbaz1' },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('receive message as target', function () {
|
|
|
|
window.metaAttributesCache.set('ol-detachRole', 'detached')
|
|
|
|
|
|
|
|
cy.mount(
|
|
|
|
<EditorProviders>
|
|
|
|
<DetachStateTest
|
|
|
|
stateKey="some-key"
|
|
|
|
defaultValue={null}
|
|
|
|
senderRole="detacher"
|
|
|
|
targetRole="detached"
|
|
|
|
handleClick={() => {}}
|
|
|
|
/>
|
|
|
|
</EditorProviders>
|
|
|
|
)
|
|
|
|
|
|
|
|
cy.wrap(null).then(() => {
|
|
|
|
testDetachChannel.postMessage({
|
|
|
|
role: 'detached',
|
|
|
|
event: 'state-some-key',
|
|
|
|
data: { value: 'barbaz2' },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
cy.get('#value').should('have.text', 'barbaz2')
|
|
|
|
})
|
|
|
|
})
|