|
1 | 1 | import React from "react";
|
| 2 | +import ReactDOM from "react-dom"; |
2 | 3 | import Month from "../src/month";
|
3 | 4 | import Day from "../src/day";
|
4 | 5 | import range from "lodash/range";
|
5 | 6 | import { mount, shallow } from "enzyme";
|
6 | 7 | import * as utils from "../src/date_utils";
|
7 | 8 | import TestUtils from "react-dom/test-utils";
|
8 | 9 |
|
| 10 | +function getKey(key) { |
| 11 | + switch (key) { |
| 12 | + case "Tab": |
| 13 | + return { key, code: 9, which: 9 }; |
| 14 | + case "Enter": |
| 15 | + return { key, code: 13, which: 13 }; |
| 16 | + case "ArrowLeft": |
| 17 | + return { key, code: 37, which: 37 }; |
| 18 | + case "ArrowRight": |
| 19 | + return { key, code: 39, which: 39 }; |
| 20 | + } |
| 21 | + throw new Error("Unknown key :" + key); |
| 22 | +} |
| 23 | + |
| 24 | + |
9 | 25 | describe("Month", () => {
|
10 | 26 | function assertDateRangeInclusive(month, start, end) {
|
11 | 27 | const dayCount = utils.getDaysDiff(end, start) + 1;
|
@@ -291,4 +307,209 @@ describe("Month", () => {
|
291 | 307 | true
|
292 | 308 | );
|
293 | 309 | });
|
| 310 | + |
| 311 | + it("should render full month name", () => { |
| 312 | + const monthComponent = mount( |
| 313 | + <Month |
| 314 | + day={utils.newDate("2015-12-01")} |
| 315 | + showMonthYearPicker |
| 316 | + showFullMonthYearPicker |
| 317 | + /> |
| 318 | + ); |
| 319 | + const month = monthComponent.find(".react-datepicker__month-1").at(0); |
| 320 | + |
| 321 | + expect(month.text()).to.equal('February'); |
| 322 | + }); |
| 323 | + |
| 324 | + it("should render short month name", () => { |
| 325 | + const monthComponent = mount( |
| 326 | + <Month |
| 327 | + day={utils.newDate("2015-12-01")} |
| 328 | + showMonthYearPicker |
| 329 | + /> |
| 330 | + ); |
| 331 | + const month = monthComponent.find(".react-datepicker__month-1").at(0); |
| 332 | + |
| 333 | + expect(month.text()).to.equal('Feb'); |
| 334 | + }); |
| 335 | + |
| 336 | + describe("Keyboard navigation", () => { |
| 337 | + const renderMonth = (props) => shallow(<Month showMonthYearPicker {...props} />); |
| 338 | + |
| 339 | + it("should trigger setPreSelection and set March as pre-selected on arrowRight", () => { |
| 340 | + let preSelected = false; |
| 341 | + const setPreSelection = param => { |
| 342 | + preSelected = param; |
| 343 | + } |
| 344 | + |
| 345 | + const monthComponent = renderMonth({ |
| 346 | + selected: utils.newDate("2015-02-01"), |
| 347 | + day: utils.newDate("2015-02-01"), |
| 348 | + setPreSelection: setPreSelection, |
| 349 | + preSelection: utils.newDate("2015-02-01"), |
| 350 | + }); |
| 351 | + monthComponent.find(".react-datepicker__month-1").simulate('keydown', getKey("Tab")); |
| 352 | + monthComponent.find(".react-datepicker__month-1").simulate('keydown', getKey("ArrowRight")); |
| 353 | + |
| 354 | + expect(preSelected.toString()).to.equal(utils.newDate("2015-03-01").toString()); |
| 355 | + }); |
| 356 | + |
| 357 | + it("should trigger setPreSelection and set January as pre-selected on arrowLeft", () => { |
| 358 | + let preSelected = false; |
| 359 | + const setPreSelection = param => { |
| 360 | + preSelected = param; |
| 361 | + } |
| 362 | + const monthComponent = renderMonth({ |
| 363 | + selected: utils.newDate("2015-02-01"), |
| 364 | + day: utils.newDate("2015-02-01"), |
| 365 | + setPreSelection: setPreSelection, |
| 366 | + preSelection: utils.newDate("2015-02-01"), |
| 367 | + }); |
| 368 | + monthComponent.find(".react-datepicker__month-1").simulate('keydown', getKey("ArrowLeft")); |
| 369 | + |
| 370 | + expect(preSelected.toString()).to.equal(utils.newDate("2015-01-01").toString()); |
| 371 | + }); |
| 372 | + |
| 373 | + it("should select March when Enter is pressed", () => { |
| 374 | + let preSelected = false; |
| 375 | + let selectedDate = null; |
| 376 | + const setPreSelection = () => { |
| 377 | + preSelected = true; |
| 378 | + } |
| 379 | + const setSelectedDate = param => { |
| 380 | + selectedDate = param; |
| 381 | + } |
| 382 | + |
| 383 | + const monthComponent = renderMonth({ |
| 384 | + selected: utils.newDate("2015-02-01"), |
| 385 | + day: utils.newDate("2015-02-01"), |
| 386 | + setPreSelection: setPreSelection, |
| 387 | + preSelection: utils.newDate("2015-02-01"), |
| 388 | + onDayClick: setSelectedDate |
| 389 | + }); |
| 390 | + |
| 391 | + monthComponent.find(".react-datepicker__month-1").simulate('keydown', getKey("ArrowLeft")); |
| 392 | + monthComponent.find(".react-datepicker__month-2").simulate('keydown', getKey("Enter")); |
| 393 | + |
| 394 | + expect(preSelected).to.equal(true); |
| 395 | + expect(selectedDate.toString()).to.equal(utils.newDate("2015-03-01").toString()); |
| 396 | + }); |
| 397 | + |
| 398 | + it("should pre-select Jan of next year on arrowRight", () => { |
| 399 | + let preSelected = false; |
| 400 | + const setPreSelection = param => { |
| 401 | + preSelected = param; |
| 402 | + } |
| 403 | + |
| 404 | + const monthComponent = renderMonth({ |
| 405 | + selected: utils.newDate("2015-12-01"), |
| 406 | + day: utils.newDate("2015-12-01"), |
| 407 | + setPreSelection: setPreSelection, |
| 408 | + preSelection: utils.newDate("2015-12-01") |
| 409 | + }); |
| 410 | + |
| 411 | + monthComponent.find(".react-datepicker__month-11").simulate('keydown', getKey("ArrowRight")); |
| 412 | + expect(preSelected.toString()).to.equal(utils.newDate("2016-01-01").toString()); |
| 413 | + }); |
| 414 | + |
| 415 | + it("should pre-select Dec of previous year on arrowLeft", () => { |
| 416 | + let preSelected = false; |
| 417 | + const setPreSelection = param => { |
| 418 | + preSelected = param; |
| 419 | + } |
| 420 | + |
| 421 | + const monthComponent = renderMonth({ |
| 422 | + selected: utils.newDate("2015-01-01"), |
| 423 | + day: utils.newDate("2015-01-01"), |
| 424 | + setPreSelection: setPreSelection, |
| 425 | + preSelection: utils.newDate("2015-01-01") |
| 426 | + }); |
| 427 | + |
| 428 | + monthComponent.find(".react-datepicker__month-0").simulate('keydown', getKey("ArrowLeft")); |
| 429 | + expect(preSelected.toString()).to.equal(utils.newDate("2014-12-01").toString()); |
| 430 | + }); |
| 431 | + |
| 432 | + it("should prevent navigation to disabled month", () => { |
| 433 | + let preSelected = utils.newDate("2015-08-01"); |
| 434 | + const setPreSelection = param => { |
| 435 | + preSelected = param; |
| 436 | + } |
| 437 | + |
| 438 | + const monthComponent = renderMonth({ |
| 439 | + selected: utils.newDate("2015-08-01"), |
| 440 | + day: utils.newDate("2015-08-01"), |
| 441 | + setPreSelection: setPreSelection, |
| 442 | + preSelection: preSelected, |
| 443 | + minDate: utils.newDate("2015-03-01"), |
| 444 | + maxDate: utils.newDate("2015-08-01") |
| 445 | + }); |
| 446 | + |
| 447 | + monthComponent.find(".react-datepicker__month-7").simulate('keydown', getKey("ArrowRight")); |
| 448 | + expect(preSelected.toString()).to.equal(utils.newDate("2015-08-01").toString()); |
| 449 | + }); |
| 450 | + |
| 451 | + it("should prevent navigation", () => { |
| 452 | + let preSelected = utils.newDate("2015-08-01"); |
| 453 | + const setPreSelection = param => { |
| 454 | + preSelected = param; |
| 455 | + } |
| 456 | + |
| 457 | + const monthComponent = renderMonth({ |
| 458 | + selected: utils.newDate("2015-08-01"), |
| 459 | + day: utils.newDate("2015-08-01"), |
| 460 | + setPreSelection: setPreSelection, |
| 461 | + preSelection: preSelected, |
| 462 | + disabledKeyboardNavigation: true |
| 463 | + }); |
| 464 | + |
| 465 | + monthComponent.find(".react-datepicker__month-7").simulate('keydown', getKey("ArrowRight")); |
| 466 | + expect(preSelected.toString()).to.equal(utils.newDate("2015-08-01").toString()); |
| 467 | + }); |
| 468 | + |
| 469 | + it("should have label for enabled/disabled month", () => { |
| 470 | + const monthComponent = renderMonth({ |
| 471 | + selected: utils.newDate("2015-03-01"), |
| 472 | + day: utils.newDate("2015-03-01"), |
| 473 | + setPreSelection: () => {}, |
| 474 | + preSelection: utils.newDate("2015-03-01"), |
| 475 | + minDate: utils.newDate("2015-03-01"), |
| 476 | + maxDate: utils.newDate("2015-08-01") |
| 477 | + }); |
| 478 | + |
| 479 | + const enabled = monthComponent |
| 480 | + .find(".react-datepicker__month-4") |
| 481 | + .at(0); |
| 482 | + |
| 483 | + const disabled = monthComponent |
| 484 | + .find(".react-datepicker__month-0") |
| 485 | + .at(0); |
| 486 | + |
| 487 | + expect(enabled.prop('aria-label')).to.equal('Choose May 2015'); |
| 488 | + expect(disabled.prop('aria-label')).to.equal('Not available January 2015'); |
| 489 | + }); |
| 490 | + |
| 491 | + it("should have custom label for month", () => { |
| 492 | + const monthComponent = renderMonth({ |
| 493 | + selected: utils.newDate("2015-03-01"), |
| 494 | + day: utils.newDate("2015-03-01"), |
| 495 | + setPreSelection: () => {}, |
| 496 | + preSelection: utils.newDate("2015-03-01"), |
| 497 | + minDate: utils.newDate("2015-03-01"), |
| 498 | + maxDate: utils.newDate("2015-08-01"), |
| 499 | + ariaLabelPrefix: "Select this", |
| 500 | + disabledDayAriaLabelPrefix: "Can't select this", |
| 501 | + }); |
| 502 | + |
| 503 | + const enabled = monthComponent |
| 504 | + .find(".react-datepicker__month-4") |
| 505 | + .at(0); |
| 506 | + |
| 507 | + const disabled = monthComponent |
| 508 | + .find(".react-datepicker__month-0") |
| 509 | + .at(0); |
| 510 | + |
| 511 | + expect(enabled.prop('aria-label')).to.equal('Select this May 2015'); |
| 512 | + expect(disabled.prop('aria-label')).to.equal(`Can't select this January 2015`); |
| 513 | + }); |
| 514 | + }); |
294 | 515 | });
|
0 commit comments