|
1 | 1 | import $ from "jquery";
|
2 | 2 | import events from "../../core/events";
|
| 3 | +import { storage } from "./date-picker"; |
3 | 4 | import pattern from "./date-picker";
|
4 | 5 | import pattern_auto_submit from "../auto-submit/auto-submit";
|
5 | 6 | import utils from "../../core/utils";
|
@@ -142,46 +143,89 @@ describe("pat-date-picker", function () {
|
142 | 143 | });
|
143 | 144 |
|
144 | 145 | describe("5 - Date picker with i18n", function () {
|
145 |
| - describe("with proper json URL", function () { |
146 |
| - it("properly localizes the months and weekdays", async () => { |
147 |
| - global.fetch = jest.fn().mockImplementation(mock_fetch_i18n); |
148 |
| - document.body.innerHTML = |
149 |
| - '<input type="date" class="pat-date-picker" value="2018-10-21" data-pat-date-picker="i18n:/path/to/i18njson" />'; |
150 |
| - const el = document.querySelector("input[type=date]"); |
151 |
| - pattern.init(el); |
152 |
| - await utils.timeout(1); // wait a tick for async to settle. |
153 |
| - const display_el = document.querySelector("time"); |
154 |
| - display_el.click(); |
155 |
| - |
156 |
| - const month = document.querySelector('.pika-lendar .pika-select-month option[selected="selected"]'); // prettier-ignore |
157 |
| - expect(month.textContent).toBe("Oktober"); |
158 |
| - |
159 |
| - global.fetch.mockClear(); |
160 |
| - delete global.fetch; |
161 |
| - }); |
| 146 | + it("with proper json URL properly localizes the months and weekdays", async () => { |
| 147 | + global.fetch = jest.fn().mockImplementation(mock_fetch_i18n); |
| 148 | + document.body.innerHTML = |
| 149 | + '<input type="date" class="pat-date-picker" value="2018-10-21" data-pat-date-picker="i18n:/path/to/i18njson" />'; |
| 150 | + const el = document.querySelector("input[type=date]"); |
| 151 | + pattern.init(el); |
| 152 | + await utils.timeout(1); // wait a tick for async to settle. |
| 153 | + const display_el = document.querySelector("time"); |
| 154 | + display_el.click(); |
| 155 | + |
| 156 | + const month = document.querySelector('.pika-lendar .pika-select-month option[selected="selected"]'); // prettier-ignore |
| 157 | + expect(month.textContent).toBe("Oktober"); |
| 158 | + |
| 159 | + // Clear the storage |
| 160 | + storage.clear(); |
| 161 | + // Reset mock |
| 162 | + global.fetch.mockClear(); |
| 163 | + delete global.fetch; |
162 | 164 | });
|
163 | 165 |
|
164 |
| - describe("with bogus json URL", function () { |
165 |
| - it("falls back to default (english) month and weekday labels ", async () => { |
166 |
| - // Simulate failing getJSON call |
167 |
| - global.fetch = jest.fn().mockImplementation(() => { |
168 |
| - throw "error"; |
169 |
| - }); |
170 |
| - |
171 |
| - document.body.innerHTML = |
172 |
| - '<input type="date" class="pat-date-picker" value="2018-10-21" data-pat-date-picker="i18n:/path/to/i18njson" />'; |
173 |
| - const el = document.querySelector("input[type=date]"); |
174 |
| - pattern.init(el); |
175 |
| - await utils.timeout(1); // wait a tick for async to settle. |
176 |
| - const display_el = document.querySelector("time"); |
177 |
| - display_el.click(); |
178 |
| - |
179 |
| - const month = document.querySelector('.pika-lendar .pika-select-month option[selected="selected"]'); // prettier-ignore |
180 |
| - expect(month.textContent).toBe("October"); |
181 |
| - |
182 |
| - global.fetch.mockClear(); |
183 |
| - delete global.fetch; |
| 166 | + it("stores i18n results", async () => { |
| 167 | + global.fetch = jest.fn().mockImplementation(mock_fetch_i18n); |
| 168 | + document.body.innerHTML = ` |
| 169 | + <input |
| 170 | + type="date" |
| 171 | + class="pat-date-picker" |
| 172 | + value="2018-10-21" |
| 173 | + data-pat-date-picker="i18n:/path/to/i18njson" |
| 174 | + /> |
| 175 | + <input |
| 176 | + type="date" |
| 177 | + class="pat-date-picker" |
| 178 | + value="2018-10-22" |
| 179 | + data-pat-date-picker="i18n:/path/to/i18njson" |
| 180 | + /> |
| 181 | + `; |
| 182 | + const els = document.querySelectorAll("input[type=date]"); |
| 183 | + pattern.init(els[0]); |
| 184 | + await utils.timeout(1); // wait a tick for async to settle. |
| 185 | + expect(global.fetch).toHaveBeenCalledTimes(1); |
| 186 | + |
| 187 | + // Initializing the other pattern should not lead to another AJAX call. |
| 188 | + |
| 189 | + // NOTE: in a real environment where multiple instances are |
| 190 | + // initialized at once on the same page, before the ajax call has |
| 191 | + // been completed, each instance will do an AJAX call. After that, |
| 192 | + // when navigating to other pages with other date picker instance |
| 193 | + // the cached value should be used and no more AJAX calls should be |
| 194 | + // made. |
| 195 | + |
| 196 | + pattern.init(els[1]); |
| 197 | + await utils.timeout(1); // wait a tick for async to settle. |
| 198 | + expect(global.fetch).toHaveBeenCalledTimes(1); |
| 199 | + |
| 200 | + // Clear the storage |
| 201 | + storage.clear(); |
| 202 | + // Reset mock |
| 203 | + global.fetch.mockClear(); |
| 204 | + delete global.fetch; |
| 205 | + }); |
| 206 | + |
| 207 | + it("with bogus json URL falls back to default (english) month and weekday labels ", async () => { |
| 208 | + // Simulate failing getJSON call |
| 209 | + global.fetch = jest.fn().mockImplementation(() => { |
| 210 | + throw "error"; |
184 | 211 | });
|
| 212 | + |
| 213 | + document.body.innerHTML = |
| 214 | + '<input type="date" class="pat-date-picker" value="2018-10-21" data-pat-date-picker="i18n:/path/to/i18njson" />'; |
| 215 | + const el = document.querySelector("input[type=date]"); |
| 216 | + console.log(document.body.innerHTML); |
| 217 | + pattern.init(el); |
| 218 | + await utils.timeout(1); // wait a tick for async to settle. |
| 219 | + console.log(document.body.innerHTML); |
| 220 | + const display_el = document.querySelector("time"); |
| 221 | + display_el.click(); |
| 222 | + |
| 223 | + const month = document.querySelector('.pika-lendar .pika-select-month option[selected="selected"]'); // prettier-ignore |
| 224 | + expect(month.textContent).toBe("October"); |
| 225 | + |
| 226 | + // Reset mock |
| 227 | + global.fetch.mockClear(); |
| 228 | + delete global.fetch; |
185 | 229 | });
|
186 | 230 | });
|
187 | 231 |
|
|
0 commit comments