|
| 1 | +jest.dontMock("moment"); |
| 2 | +import React from "react/addons"; |
| 3 | +jest.dontMock("../DateTimePickerMonths.js"); |
| 4 | +const { TestUtils } = React.addons; |
| 5 | + |
| 6 | +describe("DateTimePickerMonths", function() { |
| 7 | + const moment = require("moment"); |
| 8 | + const DateTimePickerMonths = require("../DateTimePickerMonths.js"); |
| 9 | + let subtractYearMock, addYearMock, setViewMonthMock, showYearsMock, months; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + subtractYearMock = jest.genMockFunction(); |
| 13 | + addYearMock = jest.genMockFunction(); |
| 14 | + showYearsMock = jest.genMockFunction(); |
| 15 | + setViewMonthMock = jest.genMockFunction(); |
| 16 | + months = TestUtils.renderIntoDocument( |
| 17 | + <DateTimePickerMonths |
| 18 | + addYear={addYearMock} |
| 19 | + selectedDate={moment()} |
| 20 | + setViewMonth={setViewMonthMock} |
| 21 | + showYears={showYearsMock} |
| 22 | + subtractYear={subtractYearMock} |
| 23 | + viewDate={moment()} |
| 24 | + /> |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("Controls", function() { |
| 29 | + it("calls subtractYear when clicking the prev arrow", function() { |
| 30 | + const prevArrow = TestUtils.findRenderedDOMComponentWithClass(months, "prev"); |
| 31 | + TestUtils.Simulate.click(prevArrow); |
| 32 | + expect(subtractYearMock.mock.calls.length).toBe(1); |
| 33 | + }); |
| 34 | + |
| 35 | + it("calls addYear when clicking the next arrow", function() { |
| 36 | + const nextArrow = TestUtils.findRenderedDOMComponentWithClass(months, "next"); |
| 37 | + TestUtils.Simulate.click(nextArrow); |
| 38 | + expect(addYearMock.mock.calls.length).toBe(1); |
| 39 | + }); |
| 40 | + |
| 41 | + it("calls showYears when clicking the year", function() { |
| 42 | + const year = TestUtils.findRenderedDOMComponentWithClass(months, "switch"); |
| 43 | + TestUtils.Simulate.click(year); |
| 44 | + expect(showYearsMock.mock.calls.length).toBe(1); |
| 45 | + }); |
| 46 | + |
| 47 | + it("calls setViewMonth when clicking a month", function() { |
| 48 | + const month = TestUtils.findRenderedDOMComponentWithClass(months, "active"); |
| 49 | + TestUtils.Simulate.click(month); |
| 50 | + expect(setViewMonthMock.mock.calls.length).toBe(1); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('UI', function() { |
| 55 | + it('renders 12 months', function() { |
| 56 | + const monthList = TestUtils.scryRenderedDOMComponentsWithClass(months, "month"); |
| 57 | + expect(monthList.length).toBe(12); |
| 58 | + }); |
| 59 | + |
| 60 | + it('rendersJanuary through December', function() { |
| 61 | + const monthList = TestUtils.scryRenderedDOMComponentsWithClass(months, "month"); |
| 62 | + expect(monthList.map((x) => x.props.children)).toEqual(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]); |
| 63 | + }); |
| 64 | + |
| 65 | + it("has an active month that is now's month", function() { |
| 66 | + const active = TestUtils.findRenderedDOMComponentWithClass(months, "active"); |
| 67 | + expect(active.props.children).toBe(moment().format("MMM")); |
| 68 | + }); |
| 69 | + |
| 70 | + it("has no active month that if viewDate is another year than selectedDate", function() { |
| 71 | + months = TestUtils.renderIntoDocument( |
| 72 | + <DateTimePickerMonths |
| 73 | + addYear={addYearMock} |
| 74 | + selectedDate={moment()} |
| 75 | + setViewMonth={setViewMonthMock} |
| 76 | + showYears={showYearsMock} |
| 77 | + subtractYear={subtractYearMock} |
| 78 | + viewDate={moment().add(2, 'year')} |
| 79 | + /> |
| 80 | + ); |
| 81 | + const active = TestUtils.scryRenderedDOMComponentsWithClass(months, "active"); |
| 82 | + expect(active.length).toBe(0); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | +}); |
0 commit comments