Socialify

Folder ..

Viewing date.spec.ts
235 lines (184 loc) • 8.3 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { describe, expect, it } from 'vitest';
import { faker } from '../dist/cjs';

describe('date', () => {
  describe('past()', () => {
    it('returns a date N years into the past', () => {
      const date = faker.date.past(75);
      expect(date).lessThan(new Date());
    });

    it('returns a past date when N = 0', () => {
      const refDate = new Date();
      const date = faker.date.past(0, refDate.toJSON());

      expect(date).lessThan(refDate); // date should be before the date given
    });

    it('returns a date N years before the date given', () => {
      const refDate = new Date(2120, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)

      const date = faker.date.past(75, refDate.toJSON());

      // date should be before date given but after the current time
      expect(date).lessThan(refDate);
      expect(date).greaterThan(new Date());
    });
  });

  describe('future()', () => {
    it('returns a date N years into the future', () => {
      const date = faker.date.future(75);

      expect(date).greaterThan(new Date());
    });

    it('returns a future date when N = 0', () => {
      const refDate = new Date();
      const date = faker.date.future(0, refDate.toJSON());

      expect(date).greaterThan(refDate); // date should be after the date given
    });

    it('returns a date N years after the date given', () => {
      const refDate = new Date(1880, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)

      const date = faker.date.future(75, refDate.toJSON());

      // date should be after the date given, but before the current time
      expect(date).greaterThan(refDate);
      expect(date).lessThan(new Date());
    });
  });

  describe('recent()', () => {
    it('returns a date N days from the recent past', () => {
      const date = faker.date.recent(30);

      expect(date).lessThanOrEqual(new Date());
    });

    it('returns a date N days from the recent past, starting from refDate', () => {
      const days = 30;
      const refDate = new Date(2120, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)

      const date = faker.date.recent(
        days,
        // @ts-expect-error
        refDate
      );

      const lowerBound = new Date(
        refDate.getTime() - days * 24 * 60 * 60 * 1000
      );

      expect(
        lowerBound,
        '`recent()` date should not be further back than `n` days ago'
      ).lessThanOrEqual(date);
      expect(
        date,
        '`recent()` date should not be ahead of the starting date reference'
      ).lessThanOrEqual(refDate);
    });
  });

  describe('soon()', () => {
    it('returns a date N days into the future', () => {
      const date = faker.date.soon(30);

      expect(date).greaterThanOrEqual(new Date());
    });

    it('returns a date N days from the recent future, starting from refDate', () => {
      const days = 30;
      const refDate = new Date(1880, 11, 9, 10, 0, 0, 0); // set the date beyond the usual calculation (to make sure this is working correctly)

      const date = faker.date.soon(
        days,
        // @ts-expect-error
        refDate
      );

      const upperBound = new Date(
        refDate.getTime() + days * 24 * 60 * 60 * 1000
      );

      expect(
        date,
        '`soon()` date should not be further ahead than `n` days ago'
      ).lessThanOrEqual(upperBound);
      expect(
        refDate,
        '`soon()` date should not be behind the starting date reference'
      ).lessThanOrEqual(date);
    });
  });

  describe('between()', () => {
    it('returns a random date between the dates given', () => {
      const from = new Date(1990, 5, 7, 9, 11, 0, 0);
      const to = new Date(2000, 6, 8, 10, 12, 0, 0);

      const date = faker.date.between(
        //@ts-expect-error
        from,
        to
      );

      expect(date).greaterThan(from);
      expect(date).lessThan(to);
    });
  });

  describe('betweens()', () => {
    it('returns an array of 3 dates ( by default ) of sorted randoms dates between the dates given', () => {
      const from = new Date(1990, 5, 7, 9, 11, 0, 0);
      const to = new Date(2000, 6, 8, 10, 12, 0, 0);

      const dates = faker.date.betweens(
        // @ts-expect-error
        from,
        to
      );

      expect(dates[0]).greaterThan(from);
      expect(dates[0]).lessThan(to);
      expect(dates[1]).greaterThan(dates[0]);
      expect(dates[2]).greaterThan(dates[1]);
    });
  });

  describe('month()', () => {
    it('returns random value from date.month.wide array by default', () => {
      const month = faker.date.month();
      expect(faker.definitions.date.month.wide).toContain(month);
    });

    it('returns random value from date.month.wide_context array for context option', () => {
      const month = faker.date.month({ context: true });
      expect(faker.definitions.date.month.wide_context).toContain(month);
    });

    it('returns random value from date.month.abbr array for abbr option', () => {
      const month = faker.date.month({ abbr: true });
      expect(faker.definitions.date.month.abbr).toContain(month);
    });

    it('returns random value from date.month.abbr_context array for abbr and context option', () => {
      const month = faker.date.month({ abbr: true, context: true });
      expect(faker.definitions.date.month.abbr_context).toContain(month);
    });

    it('returns random value from date.month.wide array for context option when date.month.wide_context array is missing', () => {
      const backup_wide_context = faker.definitions.date.month.wide_context;
      faker.definitions.date.month.wide_context = undefined;

      const month = faker.date.month({ context: true });
      expect(faker.definitions.date.month.wide).toContain(month);

      faker.definitions.date.month.wide_context = backup_wide_context;
    });

    it('returns random value from date.month.abbr array for abbr and context option when date.month.abbr_context array is missing', () => {
      const backup_abbr_context = faker.definitions.date.month.abbr_context;
      faker.definitions.date.month.abbr_context = undefined;

      const month = faker.date.month({ abbr: true, context: true });
      expect(faker.definitions.date.month.abbr).toContain(month);

      faker.definitions.date.month.abbr_context = backup_abbr_context;
    });
  });

  describe('weekday()', () => {
    it('returns random value from date.weekday.wide array by default', () => {
      const weekday = faker.date.weekday();
      expect(faker.definitions.date.weekday.wide).toContain(weekday);
    });

    it('returns random value from date.weekday.wide_context array for context option', () => {
      const weekday = faker.date.weekday({ context: true });
      expect(faker.definitions.date.weekday.wide_context).toContain(weekday);
    });

    it('returns random value from date.weekday.abbr array for abbr option', () => {
      const weekday = faker.date.weekday({ abbr: true });
      expect(faker.definitions.date.weekday.abbr).toContain(weekday);
    });

    it('returns random value from date.weekday.abbr_context array for abbr and context option', () => {
      const weekday = faker.date.weekday({ abbr: true, context: true });
      expect(faker.definitions.date.weekday.abbr_context).toContain(weekday);
    });

    it('returns random value from date.weekday.wide array for context option when date.weekday.wide_context array is missing', () => {
      const backup_wide_context = faker.definitions.date.weekday.wide_context;
      faker.definitions.date.weekday.wide_context = undefined;

      const weekday = faker.date.weekday({ context: true });
      expect(faker.definitions.date.weekday.wide).toContain(weekday);

      faker.definitions.date.weekday.wide_context = backup_wide_context;
    });

    it('returns random value from date.weekday.abbr array for abbr and context option when date.weekday.abbr_context array is missing', () => {
      const backup_abbr_context = faker.definitions.date.weekday.abbr_context;
      faker.definitions.date.weekday.abbr_context = undefined;

      const weekday = faker.date.weekday({ abbr: true, context: true });
      expect(faker.definitions.date.weekday.abbr).toContain(weekday);

      faker.definitions.date.weekday.abbr_context = backup_abbr_context;
    });
  });
});