Software: Apache/2.4.41 (Ubuntu). PHP/8.0.30 uname -a: Linux apirnd 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) Safe-mode: OFF (not secure) /var/www/html/wincloud_gateway/node_modules/brcast/ drwxr-xr-x | |
| Viewing file: Select action/file-type: import brcast from './index'
test('default export is a function', () => {
expect(typeof brcast).toBe('function')
})
test('throws if listener is not a function', () => {
const broadcast = brcast()
expect(() => broadcast.subscribe()).toThrow()
expect(() => broadcast.subscribe('throw')).toThrow()
expect(() => broadcast.subscribe({})).toThrow()
expect(() => broadcast.subscribe(() => {})).not.toThrow()
})
test('is able to start with an undefined state and update it accordingly', () => {
const broadcast = brcast()
expect(broadcast.getState()).toBeUndefined()
broadcast.setState(2)
expect(broadcast.getState()).toBe(2)
})
test('it updates the state', () => {
const handler = jest.fn()
const broadcast = brcast()
broadcast.subscribe(handler)
broadcast.setState(2)
expect(handler.mock.calls.length).toBe(1)
expect(handler.mock.calls[0][0]).toBe(2)
})
test('it unsubscribes only relevant listeners', () => {
const handler = jest.fn()
const handler1 = jest.fn()
const broadcast = brcast(1)
const subscription = broadcast.subscribe(handler)
broadcast.subscribe(handler1)
expect(typeof subscription).toBe('function')
subscription()
broadcast.setState(2)
broadcast.setState(3)
expect(handler.mock.calls.length).toBe(0)
expect(handler1.mock.calls.length).toBe(2)
})
test('removes listeners only once when unsubscribing more than once', () => {
const handler = jest.fn()
const broadcast = brcast(1)
const subscription = broadcast.subscribe(handler)
subscription()
subscription()
broadcast.setState(2)
expect(handler.mock.calls.length).toBe(0)
})
test('supports removing a subscription within a subscription', () => {
const broadcast = brcast(1)
const handler = jest.fn()
const handler1 = jest.fn()
const handler2 = jest.fn()
broadcast.subscribe(handler)
const unSub1 = broadcast.subscribe(() => {
handler1()
unSub1()
})
broadcast.subscribe(handler2)
broadcast.setState(2)
broadcast.setState(3)
expect(handler.mock.calls.length).toBe(2)
expect(handler1.mock.calls.length).toBe(1)
expect(handler2.mock.calls.length).toBe(2)
})
test('do not notify subscribers getting unsubscribed in the middle of a setState', () => {
const broadcast = brcast()
const unsubscribeHandles = []
const doUnsubscribeAll = () =>
unsubscribeHandles.forEach(unsubscribe => unsubscribe())
const handler = jest.fn()
const handler1 = jest.fn()
const handler2 = jest.fn()
unsubscribeHandles.push(broadcast.subscribe(handler))
unsubscribeHandles.push(
broadcast.subscribe(() => {
handler1()
doUnsubscribeAll()
})
)
unsubscribeHandles.push(broadcast.subscribe(handler2))
broadcast.setState(2)
expect(handler.mock.calls.length).toBe(1)
expect(handler1.mock.calls.length).toBe(1)
expect(handler2.mock.calls.length).toBe(0)
broadcast.setState(3)
expect(handler.mock.calls.length).toBe(1)
expect(handler1.mock.calls.length).toBe(1)
expect(handler2.mock.calls.length).toBe(0)
})
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0232 ]-- |