|
1 | 1 | import React from "react"; |
2 | 2 | import { mount } from "enzyme"; |
3 | 3 | import TimeComponent from "../src/time"; |
| 4 | +import moment from "moment"; |
4 | 5 |
|
5 | 6 | describe("TimeComponent", () => { |
6 | 7 | let sandbox; |
7 | 8 |
|
8 | 9 | beforeEach(() => { |
9 | 10 | sandbox = sinon.sandbox.create(); |
| 11 | + // mock global time to June 14, 1990 13:28:12, so test results will be constant |
| 12 | + sandbox.useFakeTimers({ |
| 13 | + now: moment("1990-06-14 13:28").valueOf(), |
| 14 | + toFake: ["Date"] |
| 15 | + }); |
10 | 16 | }); |
11 | 17 |
|
12 | 18 | afterEach(() => { |
13 | 19 | sandbox.restore(); |
14 | 20 | }); |
15 | 21 |
|
16 | | - it("should forward the time format provided in timeFormat props", () => { |
17 | | - var timeComponent = mount(<TimeComponent format="HH:mm" />); |
| 22 | + describe("Format", () => { |
| 23 | + it("should forward the time format provided in timeFormat props", () => { |
| 24 | + var timeComponent = mount(<TimeComponent format="HH:mm" />); |
18 | 25 |
|
19 | | - var timeListItem = timeComponent.find(".react-datepicker__time-list-item"); |
20 | | - expect(timeListItem.at(0).text()).to.eq("00:00"); |
| 26 | + var timeListItem = timeComponent.find( |
| 27 | + ".react-datepicker__time-list-item" |
| 28 | + ); |
| 29 | + expect(timeListItem.at(0).text()).to.eq("00:00"); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe("Initial position", () => { |
| 34 | + let spy; |
| 35 | + beforeEach(() => { |
| 36 | + spy = sandbox.spy(TimeComponent, "calcCenterPosition"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should call calcCenterPosition once", () => { |
| 40 | + mount(<TimeComponent format="HH:mm" />); |
| 41 | + expect(spy.calledOnce).to.eq(true); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should call calcCenterPosition with centerLi ref, closest to the current time", () => { |
| 45 | + mount(<TimeComponent format="HH:mm" />); |
| 46 | + expect(spy.args[0][1].innerHTML).to.eq("13:00"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should call calcCenterPosition with centerLi ref, closest to the selected time", () => { |
| 50 | + mount( |
| 51 | + <TimeComponent format="HH:mm" selected={moment("1990-06-14 08:11")} /> |
| 52 | + ); |
| 53 | + expect(spy.args[0][1].innerHTML).to.eq("08:00"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should call calcCenterPosition with centerLi ref, which is selected", () => { |
| 57 | + mount( |
| 58 | + <TimeComponent format="HH:mm" selected={moment("1990-06-14 08:00")} /> |
| 59 | + ); |
| 60 | + expect( |
| 61 | + spy.args[0][1].classList.contains( |
| 62 | + "react-datepicker__time-list-item--selected" |
| 63 | + ) |
| 64 | + ).to.be.true; |
| 65 | + }); |
| 66 | + |
| 67 | + it("should calculate scroll for the first item of 4 (even) items list", () => { |
| 68 | + expect( |
| 69 | + TimeComponent.calcCenterPosition(200, { |
| 70 | + offsetTop: 0, |
| 71 | + clientHeight: 50 |
| 72 | + }) |
| 73 | + ).to.be.eq(-75); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should calculate scroll for the last item of 4 (even) items list", () => { |
| 77 | + expect( |
| 78 | + TimeComponent.calcCenterPosition(200, { |
| 79 | + offsetTop: 150, |
| 80 | + clientHeight: 50 |
| 81 | + }) |
| 82 | + ).to.be.eq(75); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should calculate scroll for the first item of 3 (odd) items list", () => { |
| 86 | + expect( |
| 87 | + TimeComponent.calcCenterPosition(90, { offsetTop: 0, clientHeight: 30 }) |
| 88 | + ).to.be.eq(-30); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should calculate scroll for the last item of 3 (odd) items list", () => { |
| 92 | + expect( |
| 93 | + TimeComponent.calcCenterPosition(90, { |
| 94 | + offsetTop: 60, |
| 95 | + clientHeight: 30 |
| 96 | + }) |
| 97 | + ).to.be.eq(30); |
| 98 | + }); |
21 | 99 | }); |
22 | 100 | }); |
0 commit comments