|
1 | | -import { expect } from "chai"; |
2 | | -import type { Behavior } from "../observation/behavior"; |
3 | | -import { HTMLView } from "./view"; |
| 1 | +import chai, { expect } from "chai"; |
4 | 2 | import spies from "chai-spies"; |
| 3 | +import { HTMLView } from "./view"; |
5 | 4 |
|
6 | 5 | chai.use(spies); |
7 | 6 |
|
8 | 7 | describe("The HTMLView", () => { |
9 | | - function createFragment(...nodes: Node[]): DocumentFragment { |
10 | | - const fragment = document.createDocumentFragment(); |
11 | | - for (const node of nodes) { |
12 | | - fragment.appendChild(node); |
13 | | - } |
14 | | - return fragment; |
15 | | - } |
16 | | - |
17 | | - function createView( |
18 | | - nodes: Node[], |
19 | | - behaviors: Behavior[] = [] |
20 | | - ): HTMLView { |
21 | | - return new HTMLView(createFragment(...nodes), behaviors); |
22 | | - } |
23 | | - |
24 | | - it('disposeContiguousBatch disposes all views', () => { |
25 | | - const viewCount = 10; |
26 | | - const views: HTMLView[] = []; |
27 | | - const disposeSpies: ChaiSpies.Spy[] = []; |
28 | | - for (let i = 0; i < viewCount; i++) { |
29 | | - const span = document.createElement("span"); |
30 | | - const view = createView([span]); |
31 | | - views.push(view); |
32 | | - disposeSpies.push(chai.spy.on(view, "dispose")); |
33 | | - } |
34 | | - |
35 | | - HTMLView.disposeContiguousBatch(views); |
36 | | - |
37 | | - for (const spy of disposeSpies) { |
38 | | - expect(spy).to.have.been.called.exactly(1); |
39 | | - } |
40 | | - }); |
| 8 | + describe("disposeContiguousBatch", () => { |
| 9 | + function createViewWithDisposeSpy(onDispose: () => void) { |
| 10 | + const fragment = document.createDocumentFragment(); |
| 11 | + fragment.appendChild(document.createComment("start")); |
| 12 | + fragment.appendChild(document.createComment("end")); |
| 13 | + |
| 14 | + const view = new HTMLView(fragment, []); |
| 15 | + const disposeSpy = chai.spy(onDispose); |
| 16 | + chai.spy.on(view, "dispose", disposeSpy); |
| 17 | + |
| 18 | + return { view, disposeSpy }; |
| 19 | + } |
| 20 | + |
| 21 | + it("returns without throwing when given an empty batch", () => { |
| 22 | + expect(() => HTMLView.disposeContiguousBatch([])).not.to.throw(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("disposes each view in the provided batch", () => { |
| 26 | + const view1 = createViewWithDisposeSpy(() => {}); |
| 27 | + const view2 = createViewWithDisposeSpy(() => {}); |
| 28 | + const view3 = createViewWithDisposeSpy(() => {}); |
| 29 | + const views = [ |
| 30 | + view1.view, |
| 31 | + view2.view, |
| 32 | + view3.view, |
| 33 | + ]; |
| 34 | + |
| 35 | + HTMLView.disposeContiguousBatch(views); |
| 36 | + |
| 37 | + expect(view1.disposeSpy).to.have.been.called.once; |
| 38 | + expect(view2.disposeSpy).to.have.been.called.once; |
| 39 | + expect(view3.disposeSpy).to.have.been.called.once; |
| 40 | + }); |
| 41 | + }); |
41 | 42 | }); |
0 commit comments