|
| 1 | +import {expectType} from "ts-expect"; |
| 2 | +import {InvalidDateError, MaybeDate} from "../../../../src/matcher/type/date/DateMatcher"; |
| 3 | +import {IsAfterMatcher} from "../../../../src/matcher/type/date/IsAfterMatcher"; |
| 4 | +import {imock, instance, isAfter, when} from "../../../../src/ts-mockito"; |
| 5 | + |
| 6 | +describe("IsAfterMatcher", () => { |
| 7 | + describe("isAfter()", () => { |
| 8 | + it("should be a IfAfterOrEqualMatcher instance", () => { |
| 9 | + expect(isAfter(new Date()) instanceof IsAfterMatcher).toBe(true); |
| 10 | + }); |
| 11 | + |
| 12 | + it("should be a date type", () => { |
| 13 | + expectType<Date>(isAfter(new Date())); |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + describe("#constructor()", () => { |
| 18 | + it("should accept a number", () => { |
| 19 | + expect(() => new IsAfterMatcher(0)).not.toThrow(); |
| 20 | + }); |
| 21 | + |
| 22 | + it("should accept a string", () => { |
| 23 | + expect(() => new IsAfterMatcher("2020-01-01")).not.toThrow(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should accept a date", () => { |
| 27 | + expect(() => new IsAfterMatcher(new Date())).not.toThrow(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should throw an error when given value is not a valid date", () => { |
| 31 | + const date = "asdfg"; |
| 32 | + expect(() => new IsAfterMatcher(date)).toThrow(new InvalidDateError(date)); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe("#match()", () => { |
| 37 | + it("should return true when the given date is after the expected date", () => { |
| 38 | + const date = "2020-01-01"; |
| 39 | + |
| 40 | + const matcher = new IsAfterMatcher(date); |
| 41 | + |
| 42 | + const testDate = "2020-01-02"; |
| 43 | + expect(matcher.match(new Date(testDate))).toBe(true); |
| 44 | + expect(matcher.match(testDate)).toBe(true); |
| 45 | + expect(matcher.match(new Date(testDate).getTime())).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should return false when the given date is equal to the expected date", () => { |
| 49 | + const date = "2020-01-01"; |
| 50 | + |
| 51 | + const matcher = new IsAfterMatcher(date); |
| 52 | + |
| 53 | + const testDate = "2020-01-01"; |
| 54 | + expect(matcher.match(new Date(testDate))).toBe(false); |
| 55 | + expect(matcher.match(testDate)).toBe(false); |
| 56 | + expect(matcher.match(new Date(testDate).getTime())).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should return false when the given date is before the expected date", () => { |
| 60 | + const date = "2020-01-02"; |
| 61 | + |
| 62 | + const matcher = new IsAfterMatcher(date); |
| 63 | + |
| 64 | + const testDate = "2020-01-01"; |
| 65 | + expect(matcher.match(new Date(testDate))).toBe(false); |
| 66 | + expect(matcher.match(testDate)).toBe(false); |
| 67 | + expect(matcher.match(new Date(testDate).getTime())).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should return false when the given date is an invalid date", () => { |
| 71 | + const matcher = new IsAfterMatcher(new Date()); |
| 72 | + |
| 73 | + expect(matcher.match(new Date("Invalid Date"))).toBe(false); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe("#toString()", () => { |
| 78 | + it("should correctly get the string representation", () => { |
| 79 | + const date = new Date(); |
| 80 | + |
| 81 | + expect(new IsAfterMatcher(date).toString()).toBe(`isAfter(${date.toISOString()})`); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe("stubbing a method", () => { |
| 86 | + let testServiceMock: TestService; |
| 87 | + let testService: TestService; |
| 88 | + |
| 89 | + beforeEach(() => { |
| 90 | + testServiceMock = imock(); |
| 91 | + testService = instance(testServiceMock); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should pass the verification when the given date is after the expected date", () => { |
| 95 | + const date = new Date("2020-01-01"); |
| 96 | + |
| 97 | + when(testServiceMock.testMethod(isAfter(date))).thenReturn(true); |
| 98 | + |
| 99 | + const result = testService.testMethod(new Date("2020-01-02")); |
| 100 | + |
| 101 | + expect(result).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should not pass the verification when the given date is equal to the expected date", () => { |
| 105 | + const date = new Date("2020-01-01"); |
| 106 | + |
| 107 | + when(testServiceMock.testMethod(isAfter(date))).thenReturn(true); |
| 108 | + |
| 109 | + const result = testService.testMethod(new Date("2020-01-01")); |
| 110 | + |
| 111 | + expect(result).toBeNull(); |
| 112 | + }); |
| 113 | + |
| 114 | + it("should not pass the verification when the given date is before the expected date", () => { |
| 115 | + const date = new Date("2020-01-02"); |
| 116 | + |
| 117 | + when(testServiceMock.testMethod(isAfter(date))).thenReturn(true); |
| 118 | + |
| 119 | + const result = testService.testMethod(new Date("2020-01-01")); |
| 120 | + |
| 121 | + expect(result).toBeNull(); |
| 122 | + }); |
| 123 | + |
| 124 | + it("should not pass the verification false when the given date is an invalid date", () => { |
| 125 | + const date = new Date("2020-01-01"); |
| 126 | + |
| 127 | + when(testServiceMock.testMethod(isAfter(date))).thenReturn(true); |
| 128 | + |
| 129 | + const result = testService.testMethod(new Date("Invalid Date")); |
| 130 | + |
| 131 | + expect(result).toBeNull(); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +interface TestService { |
| 137 | + testMethod(date: MaybeDate): boolean; |
| 138 | +} |
0 commit comments