|
| 1 | +import { |
| 2 | + checkElementExistenceOnMount, |
| 3 | + checkOnMountAndUnmountEvents, |
| 4 | + checkOnStartEvents, |
| 5 | + checkOnStopEvents, |
| 6 | + mount |
| 7 | +} from '@src/helpers/test' |
| 8 | +import { useOrientation } from '@src/vue-use-kit' |
| 9 | + |
| 10 | +const screenOrientationMock = () => ({ |
| 11 | + angle: 0, |
| 12 | + type: 'landscape-primary' |
| 13 | +}) |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + ;(window as any).screen = { |
| 17 | + orientation: screenOrientationMock() |
| 18 | + } |
| 19 | +}) |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + jest.clearAllMocks() |
| 23 | +}) |
| 24 | + |
| 25 | +const defaultState = { |
| 26 | + angle: 0, |
| 27 | + type: 'landscape-primary' |
| 28 | +} |
| 29 | + |
| 30 | +const testComponent = (state = defaultState, onMount = true) => ({ |
| 31 | + template: ` |
| 32 | + <div> |
| 33 | + <div id="isTracking" v-if="isTracking"></div> |
| 34 | + <div id="orientation">{{JSON.stringify(orientation)}}</div> |
| 35 | + <button id="start" @click="start"></button> |
| 36 | + <button id="stop" @click="stop"></button> |
| 37 | + </div> |
| 38 | + `, |
| 39 | + setup() { |
| 40 | + const { orientation, isTracking, start, stop } = useOrientation( |
| 41 | + state, |
| 42 | + onMount |
| 43 | + ) |
| 44 | + return { orientation, isTracking, start, stop } |
| 45 | + } |
| 46 | +}) |
| 47 | + |
| 48 | +describe('useOrientation', () => { |
| 49 | + const events = ['orientationchange'] |
| 50 | + const orientationKeys = Object.keys(defaultState) |
| 51 | + const orientationValues = Object.values(defaultState) |
| 52 | + |
| 53 | + it('should add events on mounted and remove them on unmounted', async () => { |
| 54 | + await checkOnMountAndUnmountEvents(window, events, testComponent) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should add events again when start is called', async () => { |
| 58 | + await checkOnStartEvents(window, events, testComponent) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should remove events when stop is called', async () => { |
| 62 | + await checkOnStopEvents(window, events, testComponent) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should show #isTracking when runOnMount is true', async () => { |
| 66 | + await checkElementExistenceOnMount(true, testComponent(defaultState, true)) |
| 67 | + }) |
| 68 | + |
| 69 | + it('should not show #isTracking when runOnMount is false', async () => { |
| 70 | + await checkElementExistenceOnMount( |
| 71 | + false, |
| 72 | + testComponent(defaultState, false) |
| 73 | + ) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should display the orientation object keys and values', async () => { |
| 77 | + const wrapper = mount(testComponent()) |
| 78 | + await wrapper.vm.$nextTick() |
| 79 | + orientationKeys.forEach(orientationKey => { |
| 80 | + expect(wrapper.text().includes(orientationKey)).toBe(true) |
| 81 | + }) |
| 82 | + orientationValues.forEach(orientationValue => { |
| 83 | + expect(wrapper.text().includes(`${orientationValue}`)).toBe(true) |
| 84 | + }) |
| 85 | + }) |
| 86 | +}) |
0 commit comments