|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import wrap from 'jest-snapshot-serializer-raw'; |
| 9 | +import {runTest} from '../__mocks__/testUtils'; |
| 10 | +import {addEventHandler, dispatch, removeEventHandler} from '../state'; |
| 11 | + |
| 12 | +test('addEventHandler registers a callback with events', () => { |
| 13 | + const {stdout} = runTest(` |
| 14 | + let called = false; |
| 15 | + const callback = () => called = true; |
| 16 | + addEventHandler(callback); |
| 17 | +
|
| 18 | + describe('describe', () => { |
| 19 | + test('receives events', () => { |
| 20 | + console.log('called', called); |
| 21 | + expect(called).toEqual(true); |
| 22 | + }) |
| 23 | + }); |
| 24 | + `); |
| 25 | + |
| 26 | + expect(wrap(stdout)).toMatchSnapshot(); |
| 27 | +}); |
| 28 | + |
| 29 | +test('removeEventHandler unregisters a callback from events', () => { |
| 30 | + const {stdout} = runTest(` |
| 31 | + let called = false; |
| 32 | + const callback = () => called = true; |
| 33 | + addEventHandler(callback); |
| 34 | + removeEventHandler(callback); |
| 35 | +
|
| 36 | + describe('describe', () => { |
| 37 | + test('does not receive events', () => { |
| 38 | + console.log('called', called); |
| 39 | + expect(called).toEqual(false); |
| 40 | + }) |
| 41 | + }); |
| 42 | + `); |
| 43 | + |
| 44 | + expect(wrap(stdout)).toMatchSnapshot(); |
| 45 | +}); |
| 46 | + |
| 47 | +test('addEventHandler and removeEventHandler control handlers', async () => { |
| 48 | + const spy = jest.fn(); |
| 49 | + |
| 50 | + addEventHandler(spy); |
| 51 | + expect(spy).not.toHaveBeenCalledWith({name: 'unknown1'}, expect.anything()); |
| 52 | + await dispatch({name: 'unknown1' as any}); |
| 53 | + expect(spy).toHaveBeenCalledWith({name: 'unknown1'}, expect.anything()); |
| 54 | + |
| 55 | + removeEventHandler(spy); |
| 56 | + expect(spy).not.toHaveBeenCalledWith({name: 'unknown2'}, expect.anything()); |
| 57 | + await dispatch({name: 'unknown2' as any}); |
| 58 | + expect(spy).not.toHaveBeenCalledWith({name: 'unknown2'}, expect.anything()); |
| 59 | +}); |
0 commit comments