|
| 1 | +import React from 'react'; |
| 2 | +import { of } from 'rxjs'; |
| 3 | +import { View } from 'react-native'; |
| 4 | +import TestRenderer from 'react-test-renderer'; |
| 5 | + |
| 6 | +import { StopHOC } from '../StopHOC'; |
| 7 | +import { Navigation } from '../Navigation'; |
| 8 | + |
| 9 | +describe('StopHOC', () => { |
| 10 | + it('creates a Component which pipes back events and filter them based on name', () => { |
| 11 | + const spy = jest.fn(); |
| 12 | + const initialEvent = { target: 'myComponent' }; |
| 13 | + const back$ = of(initialEvent); |
| 14 | + const Stop = StopHOC({ back$ }, undefined, 'myComponent', View, undefined); |
| 15 | + const testRenderer = TestRenderer.create(<Stop />); |
| 16 | + testRenderer.root.instance.back$.subscribe(spy); |
| 17 | + expect(spy).toHaveBeenCalledWith(initialEvent); |
| 18 | + }); |
| 19 | + |
| 20 | + it('creates a Component which pipes back events and filter them out based on name', () => { |
| 21 | + const spy = jest.fn(); |
| 22 | + const initialEvent = { target: 'notMyComponent' }; |
| 23 | + const back$ = of(initialEvent); |
| 24 | + const Stop = StopHOC({ back$ }, undefined, 'myComponent', View, undefined); |
| 25 | + const testRenderer = TestRenderer.create(<Stop />); |
| 26 | + testRenderer.root.instance.back$.subscribe(spy); |
| 27 | + expect(spy).not.toHaveBeenCalled(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('creates a Component which register the onBack side effect in the Navigation\'s BackHandlerDelegate', () => { |
| 31 | + const onBackSpy = jest.fn(); |
| 32 | + const setOnBackCallbackSpy = jest.spyOn( |
| 33 | + Navigation.instance.backHandlerDelegate, |
| 34 | + 'setOnBackCallback' |
| 35 | + ); |
| 36 | + const initialEvent = { target: 'myComponent' }; |
| 37 | + const back$ = of(initialEvent); |
| 38 | + const Stop = StopHOC({ back$ }, onBackSpy, 'myComponent', View, undefined); |
| 39 | + const testRenderer = TestRenderer.create(<Stop />); |
| 40 | + testRenderer.root.instance.back$.subscribe(); |
| 41 | + expect(setOnBackCallbackSpy).toHaveBeenCalledWith(onBackSpy); |
| 42 | + }); |
| 43 | +}); |
0 commit comments