|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (C) 2016-present Chi Vinh Le and contributors. |
| 4 | + * |
| 5 | + * This software may be modified and distributed under the terms |
| 6 | + * of the MIT license. See the LICENSE file for details. |
| 7 | + */ |
| 8 | + |
| 9 | +import * as React from "react"; |
| 10 | +import { assert } from "chai"; |
| 11 | +import { shallow, ShallowWrapper } from "enzyme"; |
| 12 | +import { spy } from "sinon"; |
| 13 | +import { assemble } from "react-assemble"; |
| 14 | + |
| 15 | +import { withTransitionObserver } from "./withTransitionObserver"; |
| 16 | +import { Component } from "../../test/component"; |
| 17 | + |
| 18 | +describe("withTransitionObserver", () => { |
| 19 | + const composable = withTransitionObserver; |
| 20 | + const Assembly = assemble<any, any>(composable)(Component); |
| 21 | + const transitionInfo = { |
| 22 | + firstProperty: "width", |
| 23 | + lastProperty: "height", |
| 24 | + inTransition: true, |
| 25 | + }; |
| 26 | + let wrapper: ShallowWrapper<any, any>; |
| 27 | + let handlers = { |
| 28 | + onTransitionStart: spy(), |
| 29 | + onTransitionEnd: spy(), |
| 30 | + onTransitionBegin: spy(), |
| 31 | + onTransitionComplete: spy(), |
| 32 | + }; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + const props = { |
| 36 | + transitionInfo, |
| 37 | + ...handlers, |
| 38 | + }; |
| 39 | + wrapper = shallow(<Assembly {...props} />); |
| 40 | + Object.keys(handlers).forEach((s) => (handlers as any)[s].reset()); |
| 41 | + }); |
| 42 | + |
| 43 | + describe("onTransitionBegin", () => { |
| 44 | + it("should not call onTransitionBegin yet", () => { |
| 45 | + assert.isTrue(handlers.onTransitionBegin.notCalled); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should cal onTransitionStart", () => { |
| 49 | + const event = { |
| 50 | + target: "descendant", |
| 51 | + }; |
| 52 | + wrapper.simulate("transitionStart", event); |
| 53 | + assert.isTrue(handlers.onTransitionStart.calledWith(event)); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should not call onTransitionBegin when origin from descendants", () => { |
| 57 | + const event = { |
| 58 | + target: "descendant", |
| 59 | + }; |
| 60 | + wrapper.simulate("transitionStart", event); |
| 61 | + assert.isTrue(handlers.onTransitionBegin.notCalled); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should not call onTransitionBegin when propertyName mismatches", () => { |
| 65 | + const event = { |
| 66 | + propertyName: "foo", |
| 67 | + }; |
| 68 | + wrapper.simulate("transitionStart", event); |
| 69 | + assert.isTrue(handlers.onTransitionBegin.notCalled); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should call onTransitionBegin", () => { |
| 73 | + const event = { |
| 74 | + propertyName: "width", |
| 75 | + }; |
| 76 | + wrapper.simulate("transitionStart", event); |
| 77 | + assert.isTrue(handlers.onTransitionBegin.called); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should not call onTransitionBegin when inTransition=false", () => { |
| 81 | + const event = { |
| 82 | + propertyName: "width", |
| 83 | + }; |
| 84 | + wrapper.setProps({ transitionInfo: { inTransition: false } }); |
| 85 | + wrapper.simulate("transitionStart", event); |
| 86 | + assert.isTrue(handlers.onTransitionBegin.notCalled); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe("onTransitionComplete", () => { |
| 91 | + it("should not call onTransitionComplete yet", () => { |
| 92 | + assert.isTrue(handlers.onTransitionComplete.notCalled); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should cal onTransitionEnd", () => { |
| 96 | + const event = { |
| 97 | + target: "descendant", |
| 98 | + }; |
| 99 | + wrapper.simulate("transitionEnd", event); |
| 100 | + assert.isTrue(handlers.onTransitionEnd.calledWith(event)); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should not call onTransitionComplete when origin from descendants", () => { |
| 104 | + const event = { |
| 105 | + target: "descendant", |
| 106 | + }; |
| 107 | + wrapper.simulate("transitionEnd", event); |
| 108 | + assert.isTrue(handlers.onTransitionComplete.notCalled); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should not call onTransitionComplete when propertyName mismatches", () => { |
| 112 | + const event = { |
| 113 | + propertyName: "foo", |
| 114 | + }; |
| 115 | + wrapper.simulate("transitionEnd", event); |
| 116 | + assert.isTrue(handlers.onTransitionComplete.notCalled); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should call onTransitionComplete", () => { |
| 120 | + const event = { |
| 121 | + propertyName: "height", |
| 122 | + }; |
| 123 | + wrapper.simulate("transitionEnd", event); |
| 124 | + assert.isTrue(handlers.onTransitionComplete.called); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should not call onTransitionComplete when inTransition=false", () => { |
| 128 | + const event = { |
| 129 | + propertyName: "height", |
| 130 | + }; |
| 131 | + wrapper.setProps({ transitionInfo: { inTransition: false } }); |
| 132 | + wrapper.simulate("transitionEnd", event); |
| 133 | + assert.isTrue(handlers.onTransitionComplete.notCalled); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments