Debian Codegraph @

packages/labs/virtualizer/src/test/scenarios/testing-setup.test.ts

raw
1 /**
2 * @license
3 * Copyright 2022 Google LLC
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 import {
8 array,
9 first,
10 last,
11 ignoreBenignErrors,
12 ignoreWindowErrors,
13 pass,
14 until,
15 } from '../helpers.js';
16 import {expect} from '@open-wc/testing';
17
18 describe('array', () => {
19 it('creates an array with n items', () => {
20 const items = array(10);
21 expect(items).to.deep.equal([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
22 });
23 });
24
25 describe('first', () => {
26 it('returns the first item of an array', () => {
27 expect(first([1, 2, 3])).to.equal(1);
28 });
29
30 it('returns undefined from an empty array', () => {
31 expect(first([])).to.be.undefined;
32 });
33
34 it('returns the only item from a single item array', () => {
35 expect(first([1])).to.equal(1);
36 });
37 });
38
39 describe('last', () => {
40 it('returns the last item of an array', () => {
41 expect(last([1, 2, 3])).to.equal(3);
42 });
43
44 it('returns undefined from an empty array', () => {
45 expect(last([])).to.be.undefined;
46 });
47
48 it('returns the only item from a single item array', () => {
49 expect(last([1])).to.equal(1);
50 });
51 });
52
53 describe('ignoreBenignErrors', () => {
54 ignoreBenignErrors(beforeEach, afterEach);
55
56 // Chrome
57 it('ignores ResizeObserver loop limit exceeded error', async () => {
58 let errorThrown = false;
59 setTimeout(() => {
60 errorThrown = true;
61 throw new Error('ResizeObserver loop limit exceeded');
62 }, 0);
63 await new Promise((resolve) => setTimeout(resolve, 0));
64 expect(errorThrown).to.eq(true);
65 });
66
67 // Safari/WebKit
68 it('ignores ResizeObserver loop completed with undelivered notifications error', async () => {
69 let errorThrown = false;
70 setTimeout(() => {
71 errorThrown = true;
72 throw new Error(
73 'ResizeObserver loop completed with undelivered notifications.'
74 );
75 }, 0);
76 await new Promise((resolve) => setTimeout(resolve, 0));
77 expect(errorThrown).to.eq(true);
78 });
79
80 describe('Nested ignoreWindowErrors', () => {
81 // This ignoreWindowErrors setup is nested within the ignoreBenignErrors setup in the containing
82 // describe block. This is an expected configuration and should be handled correctly because of
83 // ordering of hooks by Mocha.
84 ignoreWindowErrors(beforeEach, afterEach, /APPLES|BANANAS/);
85
86 // Note that if we put another ignoreWindowErrors here, its afterEach will be called out
87 // of order and then the teardown will fail with an explicit error, since we need the
88 // advised method to be torn down LIFO instead of FIFO.
89
90 it('ignores APPLES error and BANANAS error and tears down in correct order', async () => {
91 let applesErrorThrown = false;
92 let bananasErrorThrown = false;
93 setTimeout(() => {
94 applesErrorThrown = true;
95 throw new Error('APPLES');
96 }, 0);
97 setTimeout(() => {
98 bananasErrorThrown = true;
99 throw new Error('BANANAS');
100 }, 0);
101 await new Promise((resolve) => setTimeout(resolve, 0));
102 expect(applesErrorThrown).to.eq(true);
103 expect(bananasErrorThrown).to.eq(true);
104 });
105 });
106 });
107
108 describe('pass', () => {
109 it('returns immediately if there are no errors', async () => {
110 let x = true;
111
112 const result = await pass(() => (x = false));
113 expect(x).to.equal(false);
114
115 // This expectation just demonstrates we don't care what the
116 // result is that is returned by this function-- i.e. not relying
117 // on a truthy value.
118 expect(result).to.be.false;
119 });
120
121 it('allows you to wait for an error-throwing Chai expectation to be true', async () => {
122 const start = new Date().getTime();
123 await pass(() => expect(start).to.be.lessThan(new Date().getTime() - 800));
124 });
125
126 it('rethrows the failed Chai expectation after timeout exceeded', async () => {
127 let err;
128 try {
129 await pass(() => expect(true).to.be.false);
130 } catch (e) {
131 err = e;
132 }
133 expect(err).to.exist;
134 expect((err as Error)!.message).to.be.eq('expected true to be false');
135 });
136 });
137
138 describe('until', () => {
139 it('resolves when condition is true', async () => {
140 let condition = false;
141 setTimeout(() => (condition = true), 100);
142 await until(() => condition);
143 expect(condition).to.be.true;
144 });
145
146 it('resolves when condition is truthy', async () => {
147 let condition: string | undefined = '';
148 setTimeout(() => (condition = 'truthy'), 100);
149 await until(() => condition);
150 expect(condition).to.eq('truthy');
151 });
152
153 it('throws an error if timeout exceeded', async () => {
154 const condition = false;
155 let error: Error;
156 try {
157 await until(() => condition);
158 } catch (e) {
159 error = e as Error;
160 }
161 expect(error!).to.exist;
162 expect(error!.message).to.contain(
163 'Condition not met within 1000ms: "() => condition"'
164 );
165 expect(error!.message).to.contain('at o.<anonymous>');
166 });
167 });