@internetarchive/collection-browser 3.3.1 → 3.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/.editorconfig +29 -29
  2. package/.github/workflows/ci.yml +27 -27
  3. package/.github/workflows/gh-pages-main.yml +39 -39
  4. package/.github/workflows/npm-publish.yml +39 -39
  5. package/.github/workflows/pr-preview.yml +38 -38
  6. package/.husky/pre-commit +4 -4
  7. package/.prettierignore +1 -1
  8. package/LICENSE +661 -661
  9. package/README.md +83 -83
  10. package/dist/src/collection-browser.js +683 -683
  11. package/dist/src/collection-browser.js.map +1 -1
  12. package/dist/src/collection-facets/more-facets-content.js +118 -118
  13. package/dist/src/collection-facets/more-facets-content.js.map +1 -1
  14. package/dist/src/collection-facets.js +265 -266
  15. package/dist/src/collection-facets.js.map +1 -1
  16. package/dist/src/data-source/collection-browser-data-source.js.map +1 -1
  17. package/dist/src/data-source/collection-browser-query-state.js.map +1 -1
  18. package/dist/src/data-source/models.js.map +1 -1
  19. package/dist/src/tiles/base-tile-component.js.map +1 -1
  20. package/dist/src/tiles/grid/account-tile.js +36 -36
  21. package/dist/src/tiles/grid/account-tile.js.map +1 -1
  22. package/dist/src/tiles/grid/collection-tile.js +77 -77
  23. package/dist/src/tiles/grid/collection-tile.js.map +1 -1
  24. package/dist/src/tiles/grid/item-tile.js +137 -137
  25. package/dist/src/tiles/grid/item-tile.js.map +1 -1
  26. package/dist/src/tiles/hover/hover-pane-controller.d.ts +9 -1
  27. package/dist/src/tiles/hover/hover-pane-controller.js +105 -37
  28. package/dist/src/tiles/hover/hover-pane-controller.js.map +1 -1
  29. package/dist/src/tiles/hover/tile-hover-pane.d.ts +1 -0
  30. package/dist/src/tiles/hover/tile-hover-pane.js +115 -112
  31. package/dist/src/tiles/hover/tile-hover-pane.js.map +1 -1
  32. package/dist/src/tiles/list/tile-list-compact.js +99 -99
  33. package/dist/src/tiles/list/tile-list-compact.js.map +1 -1
  34. package/dist/src/tiles/list/tile-list.js +297 -297
  35. package/dist/src/tiles/list/tile-list.js.map +1 -1
  36. package/dist/src/tiles/tile-dispatcher.d.ts +4 -1
  37. package/dist/src/tiles/tile-dispatcher.js +231 -204
  38. package/dist/src/tiles/tile-dispatcher.js.map +1 -1
  39. package/dist/src/utils/format-date.js.map +1 -1
  40. package/dist/test/collection-browser.test.js +189 -189
  41. package/dist/test/collection-browser.test.js.map +1 -1
  42. package/dist/test/tiles/grid/item-tile.test.js +77 -77
  43. package/dist/test/tiles/grid/item-tile.test.js.map +1 -1
  44. package/dist/test/tiles/hover/hover-pane-controller.test.js +68 -21
  45. package/dist/test/tiles/hover/hover-pane-controller.test.js.map +1 -1
  46. package/dist/test/tiles/list/tile-list-compact.test.js +70 -70
  47. package/dist/test/tiles/list/tile-list-compact.test.js.map +1 -1
  48. package/dist/test/tiles/list/tile-list.test.js +126 -126
  49. package/dist/test/tiles/list/tile-list.test.js.map +1 -1
  50. package/dist/test/tiles/tile-dispatcher.test.js +130 -52
  51. package/dist/test/tiles/tile-dispatcher.test.js.map +1 -1
  52. package/dist/test/utils/format-date.test.js.map +1 -1
  53. package/eslint.config.mjs +53 -53
  54. package/index.html +24 -24
  55. package/local.archive.org.cert +86 -86
  56. package/local.archive.org.key +27 -27
  57. package/package.json +118 -117
  58. package/renovate.json +6 -6
  59. package/src/collection-browser.ts +2829 -2829
  60. package/src/collection-facets/more-facets-content.ts +639 -639
  61. package/src/collection-facets.ts +994 -995
  62. package/src/data-source/collection-browser-data-source.ts +1401 -1401
  63. package/src/data-source/collection-browser-query-state.ts +65 -65
  64. package/src/data-source/models.ts +43 -43
  65. package/src/tiles/base-tile-component.ts +65 -65
  66. package/src/tiles/grid/account-tile.ts +113 -113
  67. package/src/tiles/grid/collection-tile.ts +163 -163
  68. package/src/tiles/grid/item-tile.ts +340 -340
  69. package/src/tiles/hover/hover-pane-controller.ts +613 -517
  70. package/src/tiles/hover/tile-hover-pane.ts +184 -180
  71. package/src/tiles/list/tile-list-compact.ts +239 -239
  72. package/src/tiles/list/tile-list.ts +700 -700
  73. package/src/tiles/tile-dispatcher.ts +517 -490
  74. package/src/utils/format-date.ts +62 -62
  75. package/test/collection-browser.test.ts +2403 -2403
  76. package/test/tiles/grid/item-tile.test.ts +520 -520
  77. package/test/tiles/hover/hover-pane-controller.test.ts +418 -353
  78. package/test/tiles/list/tile-list-compact.test.ts +282 -282
  79. package/test/tiles/list/tile-list.test.ts +552 -552
  80. package/test/tiles/tile-dispatcher.test.ts +283 -187
  81. package/test/utils/format-date.test.ts +89 -89
  82. package/tsconfig.json +20 -20
  83. package/web-dev-server.config.mjs +30 -30
  84. package/web-test-runner.config.mjs +41 -41
@@ -1,187 +1,283 @@
1
- import { aTimeout, expect, fixture } from '@open-wc/testing';
2
- import { html } from 'lit';
3
- import sinon from 'sinon';
4
- import type { TileDispatcher } from '../../src/tiles/tile-dispatcher';
5
-
6
- import '../../src/tiles/tile-dispatcher';
7
- import type { ItemTile } from '../../src/tiles/grid/item-tile';
8
- import { TileHoverPane } from '../../src/tiles/hover/tile-hover-pane';
9
- import type { HoverPaneProperties } from '../../src/tiles/hover/hover-pane-controller';
10
-
11
- describe('Tile Dispatcher', () => {
12
- it('should render item-tile for grid mode by default', async () => {
13
- const el = await fixture<TileDispatcher>(html`
14
- <tile-dispatcher
15
- .tileDisplayMode=${'grid'}
16
- .model=${{ mediatype: 'texts' }}
17
- >
18
- </tile-dispatcher>
19
- `);
20
-
21
- const itemTile = el.shadowRoot?.querySelector('item-tile');
22
- expect(itemTile).to.exist;
23
- });
24
-
25
- it('should render collection-tile for grid mode and collection mediatype', async () => {
26
- const el = await fixture<TileDispatcher>(html`
27
- <tile-dispatcher
28
- .tileDisplayMode=${'grid'}
29
- .model=${{ mediatype: 'collection' }}
30
- >
31
- </tile-dispatcher>
32
- `);
33
-
34
- const collectionTile = el.shadowRoot?.querySelector('collection-tile');
35
- expect(collectionTile).to.exist;
36
- });
37
-
38
- it('should render account-tile for grid mode and account mediatype', async () => {
39
- const el = await fixture<TileDispatcher>(html`
40
- <tile-dispatcher
41
- .tileDisplayMode=${'grid'}
42
- .model=${{ mediatype: 'account' }}
43
- >
44
- </tile-dispatcher>
45
- `);
46
-
47
- const accountTile = el.shadowRoot?.querySelector('account-tile');
48
- expect(accountTile).to.exist;
49
- });
50
-
51
- it('should render search-tile for grid mode and search mediatype', async () => {
52
- const el = await fixture<TileDispatcher>(html`
53
- <tile-dispatcher
54
- .tileDisplayMode=${'grid'}
55
- .model=${{ mediatype: 'search' }}
56
- >
57
- </tile-dispatcher>
58
- `);
59
-
60
- const searchTile = el.shadowRoot?.querySelector('search-tile');
61
- expect(searchTile).to.exist;
62
- });
63
-
64
- it('should render tile-list for extended list mode', async () => {
65
- const el = await fixture<TileDispatcher>(html`
66
- <tile-dispatcher .tileDisplayMode=${'list-detail'} .model=${{}}>
67
- </tile-dispatcher>
68
- `);
69
-
70
- const listTile = el.shadowRoot?.querySelector('tile-list');
71
- expect(listTile).to.exist;
72
- });
73
-
74
- it('should render tile-list-compact for compact list mode', async () => {
75
- const el = await fixture<TileDispatcher>(html`
76
- <tile-dispatcher .tileDisplayMode=${'list-compact'} .model=${{}}>
77
- </tile-dispatcher>
78
- `);
79
-
80
- const compactListTile = el.shadowRoot?.querySelector('tile-list-compact');
81
- expect(compactListTile).to.exist;
82
- });
83
-
84
- it('should open item in new tab when right-clicked in manage mode', async () => {
85
- const oldWindowOpen = window.open;
86
- const spy = sinon.spy();
87
- window.open = spy;
88
-
89
- const el = await fixture<TileDispatcher>(html`
90
- <tile-dispatcher
91
- isManageView
92
- .model=${{ identifier: 'foo', href: '/foo' }}
93
- .baseNavigationUrl=${''}
94
- >
95
- </tile-dispatcher>
96
- `);
97
-
98
- const tileLink = el.shadowRoot?.querySelector(
99
- 'a[href]',
100
- ) as HTMLAnchorElement;
101
- expect(tileLink).to.exist;
102
-
103
- tileLink.dispatchEvent(new Event('contextmenu'));
104
- await el.updateComplete;
105
-
106
- expect(spy.callCount).to.equal(1);
107
- expect(spy.args[0][0]).to.equal('/foo');
108
- expect(spy.args[0][1]).to.equal('_blank');
109
-
110
- window.open = oldWindowOpen;
111
- });
112
-
113
- it('should toggle model checked state when manage check clicked', async () => {
114
- const el = await fixture<TileDispatcher>(html`
115
- <tile-dispatcher
116
- isManageView
117
- .model=${{ identifier: 'foo', href: '/foo' }}
118
- .tileDisplayMode=${'grid'}
119
- ></tile-dispatcher>
120
- `);
121
-
122
- const manageCheck = el.shadowRoot?.querySelector(
123
- '.manage-check > input[type="checkbox"]',
124
- ) as HTMLButtonElement;
125
-
126
- manageCheck.click();
127
- await el.updateComplete;
128
- expect(el.model?.checked).to.be.true;
129
-
130
- manageCheck.click();
131
- await el.updateComplete;
132
- expect(el.model?.checked).to.be.false;
133
- });
134
-
135
- it('should return hover pane props', async () => {
136
- const el = await fixture<TileDispatcher>(html`
137
- <tile-dispatcher .model=${{ identifier: 'foo' }}> </tile-dispatcher>
138
- `);
139
-
140
- expect(el.getHoverPaneProps()).to.satisfy(
141
- (props: HoverPaneProperties) => props?.model?.identifier === 'foo',
142
- );
143
- });
144
-
145
- describe('Hover pane info button behavior', () => {
146
- let oldMatchMedia: typeof window.matchMedia;
147
-
148
- before(() => {
149
- oldMatchMedia = window.matchMedia;
150
- // Pretend that there is no hover-capable input device
151
- window.matchMedia = () => ({ matches: false }) as MediaQueryList;
152
- });
153
-
154
- after(() => {
155
- window.matchMedia = oldMatchMedia;
156
- });
157
-
158
- it('should toggle hover pane when tile info button is pressed', async () => {
159
- const el = await fixture<TileDispatcher>(html`
160
- <tile-dispatcher
161
- .tileDisplayMode=${'grid'}
162
- .model=${{ mediatype: 'texts' }}
163
- .enableHoverPane=${true}
164
- >
165
- </tile-dispatcher>
166
- `);
167
-
168
- const itemTile = el.shadowRoot?.querySelector('item-tile') as ItemTile;
169
- expect(itemTile).to.exist;
170
-
171
- const infoButton = itemTile.shadowRoot?.querySelector(
172
- '.info-button',
173
- ) as HTMLButtonElement;
174
- expect(infoButton).to.exist;
175
-
176
- infoButton.click();
177
- await aTimeout(500);
178
- await el.updateComplete;
179
- expect(el.getHoverPane()).to.be.instanceOf(TileHoverPane);
180
-
181
- infoButton.click();
182
- await aTimeout(500);
183
- await el.updateComplete;
184
- expect(el.getHoverPane()).not.to.exist;
185
- });
186
- });
187
- });
1
+ import { aTimeout, expect, fixture } from '@open-wc/testing';
2
+ import { html } from 'lit';
3
+ import sinon from 'sinon';
4
+ import type { TileDispatcher } from '../../src/tiles/tile-dispatcher';
5
+
6
+ import '../../src/tiles/tile-dispatcher';
7
+ import type { ItemTile } from '../../src/tiles/grid/item-tile';
8
+ import { TileHoverPane } from '../../src/tiles/hover/tile-hover-pane';
9
+ import type { HoverPaneProperties } from '../../src/tiles/hover/hover-pane-controller';
10
+
11
+ describe('Tile Dispatcher', () => {
12
+ it('should render item-tile for grid mode by default', async () => {
13
+ const el = await fixture<TileDispatcher>(html`
14
+ <tile-dispatcher
15
+ .tileDisplayMode=${'grid'}
16
+ .model=${{ mediatype: 'texts' }}
17
+ >
18
+ </tile-dispatcher>
19
+ `);
20
+
21
+ const itemTile = el.shadowRoot?.querySelector('item-tile');
22
+ expect(itemTile).to.exist;
23
+ });
24
+
25
+ it('should render collection-tile for grid mode and collection mediatype', async () => {
26
+ const el = await fixture<TileDispatcher>(html`
27
+ <tile-dispatcher
28
+ .tileDisplayMode=${'grid'}
29
+ .model=${{ mediatype: 'collection' }}
30
+ >
31
+ </tile-dispatcher>
32
+ `);
33
+
34
+ const collectionTile = el.shadowRoot?.querySelector('collection-tile');
35
+ expect(collectionTile).to.exist;
36
+ });
37
+
38
+ it('should render account-tile for grid mode and account mediatype', async () => {
39
+ const el = await fixture<TileDispatcher>(html`
40
+ <tile-dispatcher
41
+ .tileDisplayMode=${'grid'}
42
+ .model=${{ mediatype: 'account' }}
43
+ >
44
+ </tile-dispatcher>
45
+ `);
46
+
47
+ const accountTile = el.shadowRoot?.querySelector('account-tile');
48
+ expect(accountTile).to.exist;
49
+ });
50
+
51
+ it('should render search-tile for grid mode and search mediatype', async () => {
52
+ const el = await fixture<TileDispatcher>(html`
53
+ <tile-dispatcher
54
+ .tileDisplayMode=${'grid'}
55
+ .model=${{ mediatype: 'search' }}
56
+ >
57
+ </tile-dispatcher>
58
+ `);
59
+
60
+ const searchTile = el.shadowRoot?.querySelector('search-tile');
61
+ expect(searchTile).to.exist;
62
+ });
63
+
64
+ it('should render tile-list for extended list mode', async () => {
65
+ const el = await fixture<TileDispatcher>(html`
66
+ <tile-dispatcher .tileDisplayMode=${'list-detail'} .model=${{}}>
67
+ </tile-dispatcher>
68
+ `);
69
+
70
+ const listTile = el.shadowRoot?.querySelector('tile-list');
71
+ expect(listTile).to.exist;
72
+ });
73
+
74
+ it('should render tile-list-compact for compact list mode', async () => {
75
+ const el = await fixture<TileDispatcher>(html`
76
+ <tile-dispatcher .tileDisplayMode=${'list-compact'} .model=${{}}>
77
+ </tile-dispatcher>
78
+ `);
79
+
80
+ const compactListTile = el.shadowRoot?.querySelector('tile-list-compact');
81
+ expect(compactListTile).to.exist;
82
+ });
83
+
84
+ it('should open item in new tab when right-clicked in manage mode', async () => {
85
+ const oldWindowOpen = window.open;
86
+ const spy = sinon.spy();
87
+ window.open = spy;
88
+
89
+ const el = await fixture<TileDispatcher>(html`
90
+ <tile-dispatcher
91
+ isManageView
92
+ .model=${{ identifier: 'foo', href: '/foo' }}
93
+ .baseNavigationUrl=${''}
94
+ >
95
+ </tile-dispatcher>
96
+ `);
97
+
98
+ const tileLink = el.shadowRoot?.querySelector(
99
+ 'a[href]',
100
+ ) as HTMLAnchorElement;
101
+ expect(tileLink).to.exist;
102
+
103
+ tileLink.dispatchEvent(new Event('contextmenu'));
104
+ await el.updateComplete;
105
+
106
+ expect(spy.callCount).to.equal(1);
107
+ expect(spy.args[0][0]).to.equal('/foo');
108
+ expect(spy.args[0][1]).to.equal('_blank');
109
+
110
+ window.open = oldWindowOpen;
111
+ });
112
+
113
+ it('should toggle model checked state when manage check clicked', async () => {
114
+ const el = await fixture<TileDispatcher>(html`
115
+ <tile-dispatcher
116
+ isManageView
117
+ .model=${{ identifier: 'foo', href: '/foo' }}
118
+ .tileDisplayMode=${'grid'}
119
+ ></tile-dispatcher>
120
+ `);
121
+
122
+ const manageCheck = el.shadowRoot?.querySelector(
123
+ '.manage-check > input[type="checkbox"]',
124
+ ) as HTMLButtonElement;
125
+
126
+ manageCheck.click();
127
+ await el.updateComplete;
128
+ expect(el.model?.checked).to.be.true;
129
+
130
+ manageCheck.click();
131
+ await el.updateComplete;
132
+ expect(el.model?.checked).to.be.false;
133
+ });
134
+
135
+ it('should return hover pane props', async () => {
136
+ const el = await fixture<TileDispatcher>(html`
137
+ <tile-dispatcher .model=${{ identifier: 'foo' }}> </tile-dispatcher>
138
+ `);
139
+
140
+ expect(el.getHoverPaneProps()).to.satisfy(
141
+ (props: HoverPaneProperties) => props?.model?.identifier === 'foo',
142
+ );
143
+ });
144
+
145
+ it('should focus the tile link when requested', async () => {
146
+ const el = await fixture<TileDispatcher>(html`
147
+ <tile-dispatcher .tileDisplayMode=${'grid'}> </tile-dispatcher>
148
+ `);
149
+
150
+ const tileLink = el.shadowRoot?.querySelector(
151
+ '.tile-link',
152
+ ) as HTMLAnchorElement;
153
+ expect(tileLink).to.exist;
154
+
155
+ const spyFocus = sinon.spy(tileLink, 'focus');
156
+ el.acquireFocus();
157
+ expect(spyFocus.callCount).to.equal(1);
158
+ });
159
+
160
+ it('should blur the tile link when requested', async () => {
161
+ const el = await fixture<TileDispatcher>(html`
162
+ <tile-dispatcher .tileDisplayMode=${'grid'}> </tile-dispatcher>
163
+ `);
164
+
165
+ const tileLink = el.shadowRoot?.querySelector(
166
+ '.tile-link',
167
+ ) as HTMLAnchorElement;
168
+ expect(tileLink).to.exist;
169
+
170
+ const spyBlur = sinon.spy(tileLink, 'blur');
171
+ el.releaseFocus();
172
+ expect(spyBlur.callCount).to.equal(1);
173
+ });
174
+
175
+ describe('Hover pane info button behavior', () => {
176
+ let oldMatchMedia: typeof window.matchMedia;
177
+
178
+ before(() => {
179
+ oldMatchMedia = window.matchMedia;
180
+ // Pretend that there is no hover-capable input device
181
+ window.matchMedia = () => ({ matches: false }) as MediaQueryList;
182
+ });
183
+
184
+ after(() => {
185
+ window.matchMedia = oldMatchMedia;
186
+ });
187
+
188
+ it('should toggle hover pane when tile info button is pressed', async () => {
189
+ const el = await fixture<TileDispatcher>(html`
190
+ <tile-dispatcher
191
+ .tileDisplayMode=${'grid'}
192
+ .model=${{ mediatype: 'texts' }}
193
+ .enableHoverPane=${true}
194
+ >
195
+ </tile-dispatcher>
196
+ `);
197
+
198
+ const itemTile = el.shadowRoot?.querySelector('item-tile') as ItemTile;
199
+ expect(itemTile).to.exist;
200
+
201
+ const infoButton = itemTile.shadowRoot?.querySelector(
202
+ '.info-button',
203
+ ) as HTMLButtonElement;
204
+ expect(infoButton).to.exist;
205
+
206
+ infoButton.click();
207
+ await aTimeout(500);
208
+ await el.updateComplete;
209
+ expect(el.getHoverPane()).to.be.instanceOf(TileHoverPane);
210
+
211
+ infoButton.click();
212
+ await aTimeout(500);
213
+ await el.updateComplete;
214
+ expect(el.getHoverPane()).not.to.exist;
215
+ });
216
+ });
217
+
218
+ describe('Accessibility', () => {
219
+ it('should have proper aria-label on tile link', async () => {
220
+ const el = await fixture<TileDispatcher>(html`
221
+ <tile-dispatcher
222
+ .tileDisplayMode=${'grid'}
223
+ .model=${{
224
+ title: 'Example Title',
225
+ mediatype: 'texts',
226
+ }}
227
+ >
228
+ </tile-dispatcher>
229
+ `);
230
+
231
+ const tileLink = el.shadowRoot?.querySelector(
232
+ '.tile-link',
233
+ ) as HTMLAnchorElement;
234
+ expect(tileLink).to.exist;
235
+ expect(tileLink.getAttribute('aria-label')).to.equal('Example Title');
236
+ });
237
+
238
+ it('should fallback to untitled aria-label on tile link when no title', async () => {
239
+ const el = await fixture<TileDispatcher>(html`
240
+ <tile-dispatcher
241
+ .tileDisplayMode=${'grid'}
242
+ .model=${{
243
+ mediatype: 'texts',
244
+ }}
245
+ >
246
+ </tile-dispatcher>
247
+ `);
248
+
249
+ const tileLink = el.shadowRoot?.querySelector(
250
+ '.tile-link',
251
+ ) as HTMLAnchorElement;
252
+ expect(tileLink).to.exist;
253
+ expect(tileLink.getAttribute('aria-label')).to.equal('Untitled item');
254
+ });
255
+
256
+ it('should have no accessibility violations in grid mode', async () => {
257
+ const el = await fixture<TileDispatcher>(html`
258
+ <tile-dispatcher
259
+ .tileDisplayMode=${'grid'}
260
+ .model=${{
261
+ title: 'Example Title',
262
+ mediatype: 'texts',
263
+ }}
264
+ >
265
+ </tile-dispatcher>
266
+ `);
267
+
268
+ await expect(el).to.be.accessible();
269
+ });
270
+
271
+ it('should have no accessibility violations in list mode', async () => {
272
+ const el = await fixture<TileDispatcher>(html`
273
+ <tile-dispatcher
274
+ .tileDisplayMode=${'list-detail'}
275
+ .model=${{ mediatype: 'texts' }}
276
+ >
277
+ </tile-dispatcher>
278
+ `);
279
+
280
+ await expect(el).to.be.accessible();
281
+ });
282
+ });
283
+ });
@@ -1,89 +1,89 @@
1
- import { expect } from '@open-wc/testing';
2
- import { formatDate } from '../../src/utils/format-date';
3
-
4
- const testDate = new Date(2020, 11, 9);
5
-
6
- describe('formatDate', () => {
7
- it('returns blank when undefined date', () => {
8
- expect(formatDate(undefined)).to.equal('');
9
- });
10
-
11
- it('returns short date when no DateFormat', () => {
12
- expect(formatDate(testDate)).to.equal('Dec 2020');
13
- });
14
-
15
- it('returns long date when long DateFormat', () => {
16
- expect(formatDate(testDate, 'long')).to.equal('Dec 09, 2020');
17
- });
18
-
19
- it('returns year-only date when year-only DateFormat', () => {
20
- expect(formatDate(testDate, 'year-only')).to.equal('2020');
21
- });
22
-
23
- it('returns correct year for old "Jan 1 at midnight" dates and year-only DateFormat', () => {
24
- // Many standard timezones have a discontinuity in date parsing at some point during
25
- // the 19th or 20th century, corresponding to the creation of the timezone.
26
- // Dates prior to the discontinuity generally have a non-hour-aligned timezone offset
27
- // which can throw off the calculated year for dates which are close to a year boundary.
28
- // This is particularly problematic for "Jan 1 at midnight" dates, which are what we
29
- // receive from the search engine for date metadata that only specifies the year.
30
- // So we must ensure these older dates still output the correct year, not the prior one.
31
- expect(formatDate(new Date('1234-01-01T00:00:00Z'), 'year-only')).to.equal(
32
- '1234',
33
- );
34
- });
35
-
36
- it('uses UTC time zone by default or when useLocalTime is explicitly false', () => {
37
- // Default options
38
- expect(formatDate(new Date('2025-02-15T00:00:00Z'), 'long')).to.equal(
39
- 'Feb 15, 2025',
40
- );
41
- expect(formatDate(new Date('2025-02-15T23:59:59Z'), 'long')).to.equal(
42
- 'Feb 15, 2025',
43
- );
44
-
45
- // Explicit `useLocalTime: false` option
46
- const options = { useLocalTime: false };
47
- expect(
48
- formatDate(new Date('2025-02-15T00:00:00Z'), 'long', options),
49
- ).to.equal('Feb 15, 2025');
50
- expect(
51
- formatDate(new Date('2025-02-15T23:59:59Z'), 'long', options),
52
- ).to.equal('Feb 15, 2025');
53
- });
54
-
55
- it('uses local time zone when specified', () => {
56
- // N.B.:
57
- // - Positive offset corresponds to UTC-x zones
58
- // - Negative offset corresponds to UTC+x zones
59
- const offset = new Date().getTimezoneOffset();
60
- const options = { useLocalTime: true };
61
-
62
- // The expected behavior depends on the local time where the tests are run:
63
- if (offset > 0) {
64
- // If we're testing under a positive offset, the first second of the UTC day should locally fall on the previous day
65
- expect(
66
- formatDate(new Date('2025-02-15T00:00:00Z'), 'long', options),
67
- ).to.equal('Feb 14, 2025');
68
- } else if (offset < 0) {
69
- // If we're testing under a negative offset, the last second of the UTC day should locally fall on the next day
70
- expect(
71
- formatDate(new Date('2025-02-15T23:59:59Z'), 'long', options),
72
- ).to.equal('Feb 16, 2025');
73
- } else {
74
- // If we're testing *in* UTC, then both seconds should locally fall on the same day
75
- expect(
76
- formatDate(new Date('2025-02-15T00:00:00Z'), 'long', options),
77
- ).to.equal('Feb 15, 2025');
78
- expect(
79
- formatDate(new Date('2025-02-15T23:59:59Z'), 'long', options),
80
- ).to.equal('Feb 15, 2025');
81
- }
82
- });
83
-
84
- it('returns locale formatted date', () => {
85
- expect(formatDate(testDate, 'long', { locale: 'de-DE' })).to.equal(
86
- '09. Dez. 2020',
87
- );
88
- });
89
- });
1
+ import { expect } from '@open-wc/testing';
2
+ import { formatDate } from '../../src/utils/format-date';
3
+
4
+ const testDate = new Date(2020, 11, 9);
5
+
6
+ describe('formatDate', () => {
7
+ it('returns blank when undefined date', () => {
8
+ expect(formatDate(undefined)).to.equal('');
9
+ });
10
+
11
+ it('returns short date when no DateFormat', () => {
12
+ expect(formatDate(testDate)).to.equal('Dec 2020');
13
+ });
14
+
15
+ it('returns long date when long DateFormat', () => {
16
+ expect(formatDate(testDate, 'long')).to.equal('Dec 09, 2020');
17
+ });
18
+
19
+ it('returns year-only date when year-only DateFormat', () => {
20
+ expect(formatDate(testDate, 'year-only')).to.equal('2020');
21
+ });
22
+
23
+ it('returns correct year for old "Jan 1 at midnight" dates and year-only DateFormat', () => {
24
+ // Many standard timezones have a discontinuity in date parsing at some point during
25
+ // the 19th or 20th century, corresponding to the creation of the timezone.
26
+ // Dates prior to the discontinuity generally have a non-hour-aligned timezone offset
27
+ // which can throw off the calculated year for dates which are close to a year boundary.
28
+ // This is particularly problematic for "Jan 1 at midnight" dates, which are what we
29
+ // receive from the search engine for date metadata that only specifies the year.
30
+ // So we must ensure these older dates still output the correct year, not the prior one.
31
+ expect(formatDate(new Date('1234-01-01T00:00:00Z'), 'year-only')).to.equal(
32
+ '1234',
33
+ );
34
+ });
35
+
36
+ it('uses UTC time zone by default or when useLocalTime is explicitly false', () => {
37
+ // Default options
38
+ expect(formatDate(new Date('2025-02-15T00:00:00Z'), 'long')).to.equal(
39
+ 'Feb 15, 2025',
40
+ );
41
+ expect(formatDate(new Date('2025-02-15T23:59:59Z'), 'long')).to.equal(
42
+ 'Feb 15, 2025',
43
+ );
44
+
45
+ // Explicit `useLocalTime: false` option
46
+ const options = { useLocalTime: false };
47
+ expect(
48
+ formatDate(new Date('2025-02-15T00:00:00Z'), 'long', options),
49
+ ).to.equal('Feb 15, 2025');
50
+ expect(
51
+ formatDate(new Date('2025-02-15T23:59:59Z'), 'long', options),
52
+ ).to.equal('Feb 15, 2025');
53
+ });
54
+
55
+ it('uses local time zone when specified', () => {
56
+ // N.B.:
57
+ // - Positive offset corresponds to UTC-x zones
58
+ // - Negative offset corresponds to UTC+x zones
59
+ const offset = new Date().getTimezoneOffset();
60
+ const options = { useLocalTime: true };
61
+
62
+ // The expected behavior depends on the local time where the tests are run:
63
+ if (offset > 0) {
64
+ // If we're testing under a positive offset, the first second of the UTC day should locally fall on the previous day
65
+ expect(
66
+ formatDate(new Date('2025-02-15T00:00:00Z'), 'long', options),
67
+ ).to.equal('Feb 14, 2025');
68
+ } else if (offset < 0) {
69
+ // If we're testing under a negative offset, the last second of the UTC day should locally fall on the next day
70
+ expect(
71
+ formatDate(new Date('2025-02-15T23:59:59Z'), 'long', options),
72
+ ).to.equal('Feb 16, 2025');
73
+ } else {
74
+ // If we're testing *in* UTC, then both seconds should locally fall on the same day
75
+ expect(
76
+ formatDate(new Date('2025-02-15T00:00:00Z'), 'long', options),
77
+ ).to.equal('Feb 15, 2025');
78
+ expect(
79
+ formatDate(new Date('2025-02-15T23:59:59Z'), 'long', options),
80
+ ).to.equal('Feb 15, 2025');
81
+ }
82
+ });
83
+
84
+ it('returns locale formatted date', () => {
85
+ expect(formatDate(testDate, 'long', { locale: 'de-DE' })).to.equal(
86
+ '09. Dez. 2020',
87
+ );
88
+ });
89
+ });