|  | 
| 1 | 1 | import $ from "jquery"; | 
| 2 | 2 | import dom from "./dom"; | 
|  | 3 | +import utils from "./utils"; | 
| 3 | 4 | 
 | 
| 4 | 5 | describe("core.dom tests", () => { | 
| 5 | 6 |     // Tests from the core.dom module | 
| 6 | 7 | 
 | 
| 7 | 8 |     afterEach(() => { | 
| 8 | 9 |         document.body.innerHTML = ""; | 
|  | 10 | +        jest.restoreAllMocks(); | 
|  | 11 | +    }); | 
|  | 12 | + | 
|  | 13 | +    describe("document_ready", () => { | 
|  | 14 | +        it("calls the callback, once the document is ready.", async () => { | 
|  | 15 | +            let cnt = 0; | 
|  | 16 | +            const counter = () => { | 
|  | 17 | +                cnt++; | 
|  | 18 | +            }; | 
|  | 19 | + | 
|  | 20 | +            // Call document ready immediately. It should already call the | 
|  | 21 | +            // callback, if ready. Which it isn't. | 
|  | 22 | +            jest.spyOn(document, "readyState", "get").mockReturnValue("loading"); | 
|  | 23 | +            dom.document_ready(counter); | 
|  | 24 | +            await utils.timeout(1); | 
|  | 25 | +            expect(cnt).toBe(0); | 
|  | 26 | + | 
|  | 27 | +            // While readyState "loading" the callback should not be called. | 
|  | 28 | +            document.dispatchEvent(new Event("readystatechange")); | 
|  | 29 | +            await utils.timeout(1); | 
|  | 30 | +            expect(cnt).toBe(0); | 
|  | 31 | + | 
|  | 32 | +            // While still loading the callback should still not be called. | 
|  | 33 | +            document.dispatchEvent(new Event("readystatechange")); | 
|  | 34 | +            await utils.timeout(1); | 
|  | 35 | +            expect(cnt).toBe(0); | 
|  | 36 | + | 
|  | 37 | +            // Now it's the time. | 
|  | 38 | +            jest.spyOn(document, "readyState", "get").mockReturnValue("complete"); | 
|  | 39 | +            document.dispatchEvent(new Event("readystatechange")); | 
|  | 40 | +            await utils.timeout(1); | 
|  | 41 | +            expect(cnt).toBe(1); | 
|  | 42 | + | 
|  | 43 | +            // But the callback is only called once and the event handler removed from the document. | 
|  | 44 | +            document.dispatchEvent(new Event("readystatechange")); | 
|  | 45 | +            await utils.timeout(1); | 
|  | 46 | +            expect(cnt).toBe(1); | 
|  | 47 | +        }); | 
|  | 48 | + | 
|  | 49 | +        it("it will also fire on readyState interactive, not only complete.", async () => { | 
|  | 50 | +            let cnt = 0; | 
|  | 51 | +            const counter = () => { | 
|  | 52 | +                cnt++; | 
|  | 53 | +            }; | 
|  | 54 | + | 
|  | 55 | +            // Call document ready immediately. It should already call the | 
|  | 56 | +            // callback, if ready. Which it isn't. | 
|  | 57 | +            jest.spyOn(document, "readyState", "get").mockReturnValue("loading"); | 
|  | 58 | +            dom.document_ready(counter); | 
|  | 59 | +            await utils.timeout(1); | 
|  | 60 | +            expect(cnt).toBe(0); | 
|  | 61 | + | 
|  | 62 | +            // When readyState interactive, the callback should be called. | 
|  | 63 | +            jest.spyOn(document, "readyState", "get").mockReturnValue("interactive"); | 
|  | 64 | +            document.dispatchEvent(new Event("readystatechange")); | 
|  | 65 | +            await utils.timeout(1); | 
|  | 66 | +            expect(cnt).toBe(1); | 
|  | 67 | +        }); | 
|  | 68 | + | 
|  | 69 | +        it("the callback will be called immedeately if the ready state change has already happended.", async () => { | 
|  | 70 | +            let cnt = 0; | 
|  | 71 | +            const counter = () => { | 
|  | 72 | +                cnt++; | 
|  | 73 | +            }; | 
|  | 74 | + | 
|  | 75 | +            // Call document ready immediately. It should already call the | 
|  | 76 | +            // callback, if ready. Which it isn't. | 
|  | 77 | +            jest.spyOn(document, "readyState", "get").mockReturnValue("complete"); | 
|  | 78 | +            dom.document_ready(counter); | 
|  | 79 | +            await utils.timeout(1); | 
|  | 80 | +            expect(cnt).toBe(1); | 
|  | 81 | + | 
|  | 82 | +            // But another state change would not call the callback, because | 
|  | 83 | +            // the event listener is already de-registered. | 
|  | 84 | +            jest.spyOn(document, "readyState", "get").mockReturnValue("interactive"); | 
|  | 85 | +            document.dispatchEvent(new Event("readystatechange")); | 
|  | 86 | +            await utils.timeout(1); | 
|  | 87 | +            expect(cnt).toBe(1); | 
|  | 88 | +        }); | 
| 9 | 89 |     }); | 
| 10 | 90 | 
 | 
| 11 | 91 |     describe("toNodeArray tests", () => { | 
|  | 
0 commit comments