|
| 1 | +import React from 'react'; |
| 2 | +import { mount } from 'enzyme'; |
| 3 | +import raf from 'raf'; |
| 4 | +import Popup from '../src/Popup'; |
| 5 | + |
| 6 | +jest.mock('raf', () => { |
| 7 | + const rafMock = jest.fn(() => 1); |
| 8 | + rafMock.cancel = jest.fn(); |
| 9 | + return rafMock; |
| 10 | +}); |
| 11 | + |
| 12 | +describe('Popup', () => { |
| 13 | + afterEach(() => { |
| 14 | + raf.mockClear(); |
| 15 | + raf.cancel.mockClear(); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('Popup getDerivedStateFromProps status behavior', () => { |
| 19 | + it('returns stable on init', () => { |
| 20 | + const props = { visible: false }; |
| 21 | + const state = { prevVisible: null, status: 'something' }; |
| 22 | + |
| 23 | + expect(Popup.getDerivedStateFromProps(props, state).status).toBe('stable'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('does not change when visible is unchanged', () => { |
| 27 | + const props = { visible: true }; |
| 28 | + const state = { prevVisible: true, status: 'something' }; |
| 29 | + |
| 30 | + expect(Popup.getDerivedStateFromProps(props, state).status).toBe('something'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns null when visible is changed to true', () => { |
| 34 | + const props = { visible: true }; |
| 35 | + const state = { prevVisible: false, status: 'something' }; |
| 36 | + |
| 37 | + expect(Popup.getDerivedStateFromProps(props, state).status).toBe(null); |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns stable when visible is changed to false and motion is not supported', () => { |
| 41 | + const props = { visible: false }; |
| 42 | + const state = { prevVisible: true, status: 'something' }; |
| 43 | + |
| 44 | + expect(Popup.getDerivedStateFromProps(props, state).status).toBe('stable'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('returns null when visible is changed to false and motion is started', () => { |
| 48 | + const props = { |
| 49 | + visible: false, |
| 50 | + motion: { |
| 51 | + motionName: 'enter', |
| 52 | + }, |
| 53 | + }; |
| 54 | + const state = { prevVisible: true, status: 'motion' }; |
| 55 | + |
| 56 | + expect(Popup.getDerivedStateFromProps(props, state).status).toBe(null); |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns stable when visible is changed to false and motion is not started', () => { |
| 60 | + const props = { |
| 61 | + visible: false, |
| 62 | + motion: { |
| 63 | + motionName: 'enter', |
| 64 | + }, |
| 65 | + }; |
| 66 | + const state = { prevVisible: true, status: 'beforeMotion' }; |
| 67 | + |
| 68 | + expect(Popup.getDerivedStateFromProps(props, state).status).toBe('stable'); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('Popup cancels pending animation frames on update', () => { |
| 73 | + const wrapper = mount( |
| 74 | + <Popup visible motion={{}}> |
| 75 | + <div>popup content</div> |
| 76 | + </Popup>, |
| 77 | + ); |
| 78 | + |
| 79 | + expect(raf).toHaveBeenCalledTimes(1); |
| 80 | + |
| 81 | + raf.cancel.mockClear(); |
| 82 | + wrapper.setProps({ visible: false }); |
| 83 | + expect(raf.cancel).toHaveBeenCalledTimes(1); |
| 84 | + }); |
| 85 | +}); |
0 commit comments