Skip to content

Commit

Permalink
chore: improve test cases (#14984)
Browse files Browse the repository at this point in the history
* chore: improve test cases

* lint
  • Loading branch information
trueadm authored Jan 13, 2025
1 parent ab3290f commit 36ef59d
Show file tree
Hide file tree
Showing 38 changed files with 164 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { test } from '../../assert';
const tick = () => Promise.resolve();

Expand All @@ -14,8 +15,7 @@ export default test({
el.setAttribute('camel-case', 'universe');
el.setAttribute('an-array', '[3,4]');
el.setAttribute('camelcase2', 'Hi');
await tick();
await tick();
flushSync();
assert.htmlEqual(el.shadowRoot.innerHTML, '<h1>Hi universe!</h1> <p>3</p><p>4</p>');
assert.htmlEqual(
target.innerHTML,
Expand All @@ -25,8 +25,7 @@ export default test({
el.camelCase = 'galaxy';
el.camelCase2 = 'Hey';
el.anArray = [5, 6];
await tick();
await tick();
flushSync();
assert.htmlEqual(el.shadowRoot.innerHTML, '<h1>Hey galaxy!</h1> <p>5</p><p>6</p>');
assert.htmlEqual(
target.innerHTML,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { test } from '../../assert';
const tick = () => Promise.resolve();

Expand All @@ -8,7 +9,8 @@ export default test({
/** @type {any} */
const el = target.querySelector('custom-element');

await el.updateFoo(42);
el.updateFoo(42);
flushSync();

const p = el.shadowRoot.querySelector('p');
assert.equal(p.textContent, '42');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test } from '../../assert';
import { flushSync } from 'svelte';
const tick = () => Promise.resolve();

export default test({
Expand All @@ -16,7 +17,7 @@ export default test({
assert.equal(p.innerHTML, 'false');

button.click();
await tick();
flushSync();

assert.equal(button.innerHTML, '1');
assert.equal(p.innerHTML, 'false');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
Expand All @@ -10,12 +11,13 @@ export default test({
<p>second: </p>
`,

async test({ assert, component, target, window }) {
test({ assert, component, target, window }) {
const event = new window.MouseEvent('click');

const buttons = target.querySelectorAll('button');

await buttons[1].dispatchEvent(event);
buttons[1].dispatchEvent(event);
flushSync();

assert.htmlEqual(
target.innerHTML,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
Expand All @@ -10,12 +11,13 @@ export default test({
<p>fromState: </p>
`,

async test({ assert, component, target, window }) {
test({ assert, component, target, window }) {
const event = new window.MouseEvent('click');

const buttons = target.querySelectorAll('button');

await buttons[1].dispatchEvent(event);
buttons[1].dispatchEvent(event);
flushSync();

assert.htmlEqual(
target.innerHTML,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { flushSync } from 'svelte';
import { ok, test } from '../../test';

export default test({
mode: ['client', 'hydrate'],

html: '<button>10</button>',

async test({ assert, target, window }) {
test({ assert, target, window }) {
const event = new window.MouseEvent('click');

const button = target.querySelector('button');
ok(button);

await button.dispatchEvent(event);
button.dispatchEvent(event);
flushSync();

assert.htmlEqual(target.innerHTML, '<button>11</button>');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { flushSync } from 'svelte';
import { ok, test } from '../../test';

export default test({
html: '<button>???</button>',

async test({ assert, target, window }) {
test({ assert, target, window }) {
const event = new window.MouseEvent('click', {
clientX: 42,
clientY: 42
Expand All @@ -12,7 +13,8 @@ export default test({
const button = target.querySelector('button');
ok(button);

await button.dispatchEvent(event);
button.dispatchEvent(event);
flushSync();

assert.htmlEqual(target.innerHTML, '<button>42</button>');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { flushSync } from 'svelte';
import { ok, test } from '../../test';

export default test({
html: '<button>0, 0</button>',

async test({ assert, target, window }) {
test({ assert, target, window }) {
const event = new window.MouseEvent('click', {
clientX: 42,
clientY: 42
Expand All @@ -12,7 +13,8 @@ export default test({
const button = target.querySelector('button');
ok(button);

await button.dispatchEvent(event);
button.dispatchEvent(event);
flushSync();

assert.htmlEqual(target.innerHTML, '<button>42, 42</button>');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { ok, test } from '../../test';

export default test({
Expand All @@ -8,7 +9,8 @@ export default test({
const click = new window.MouseEvent('click');

assert.htmlEqual(target.innerHTML, '<button>1</button>');
await button.dispatchEvent(click);
button.dispatchEvent(click);
flushSync();
assert.htmlEqual(target.innerHTML, '<button>2</button>');
}
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
Expand All @@ -11,7 +12,9 @@ export default test({
const [b1] = target.querySelectorAll('button');

b1.click();
await Promise.resolve();
Promise.resolve();
flushSync();

assert.htmlEqual(
target.innerHTML,
`<button>2</button><button>3</button><button>4</button>\n-------\n<button>1</button>`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
Expand All @@ -11,6 +12,7 @@ export default test({

async test({ assert, component, target }) {
await (component.thePromise = Promise.resolve({ result: 1 }));
flushSync();

assert.htmlEqual(
target.innerHTML,
Expand All @@ -21,6 +23,7 @@ export default test({
);

await new Promise((resolve) => setTimeout(resolve, 1));
flushSync();

assert.htmlEqual(
target.innerHTML,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
Expand All @@ -9,8 +10,9 @@ export default test({
<span>1</span>
`,

async test({ assert, component, target }) {
await component.list.update();
test({ assert, component, target }) {
component.list.update();
flushSync();

assert.htmlEqual(
target.innerHTML,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { ok, test } from '../../test';

export default test({
Expand All @@ -18,7 +19,7 @@ export default test({
<p>hello</p>
`,

async test({ assert, component, target, window }) {
test({ assert, component, target, window }) {
assert.equal(component.name, 'world');

const el = target.querySelector('editor');
Expand All @@ -27,7 +28,8 @@ export default test({
const event = new window.Event('input');

el.textContent = 'everybody';
await el.dispatchEvent(event);
el.dispatchEvent(event);
flushSync();

assert.htmlEqual(
target.innerHTML,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { ok, test } from '../../test';

export default test({
Expand All @@ -10,15 +11,16 @@ export default test({
<p>hello world</p>
`,

async test({ assert, component, target, window }) {
test({ assert, component, target, window }) {
const el = target.querySelector('editor');
ok(el);
assert.equal(el.textContent, 'world');

const event = new window.Event('input');

el.textContent = 'everybody';
await el.dispatchEvent(event);
el.dispatchEvent(event);
flushSync();

assert.htmlEqual(
target.innerHTML,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { ok, test } from '../../test';

export default test({
Expand All @@ -17,7 +18,7 @@ export default test({
<p>indeterminate? true</p>
`,

async test({ assert, component, target, window }) {
test({ assert, component, target, window }) {
const input = target.querySelector('input');
ok(input);

Expand All @@ -28,7 +29,8 @@ export default test({

input.checked = true;
input.indeterminate = false;
await input.dispatchEvent(event);
input.dispatchEvent(event);
flushSync();

assert.equal(component.indeterminate, false);
assert.equal(component.checked, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { ok, test } from '../../test';

export default test({
Expand All @@ -7,8 +8,9 @@ export default test({
return { letter: 'b' };
},

async test({ assert, component, target, window }) {
await component.modal.toggle();
test({ assert, component, target, window }) {
component.modal.toggle();
flushSync();

assert.htmlEqual(
target.innerHTML,
Expand All @@ -28,7 +30,8 @@ export default test({
const change = new window.MouseEvent('change');

select.options[2].selected = true;
await select.dispatchEvent(change);
select.dispatchEvent(change);
flushSync();
assert.equal(component.letter, 'c');

assert.deepEqual(
Expand All @@ -49,9 +52,9 @@ export default test({
`
);

await component.modal.toggle();
await component.modal.toggle();
await Promise.resolve();
component.modal.toggle();
component.modal.toggle();
flushSync();

select = target.querySelector('select');
ok(select);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, component }) {
test({ assert, component }) {
const { foo, p } = component;

/** @type {string[]} */
Expand All @@ -13,7 +14,8 @@ export default test({
}
});

await foo.double();
foo.double();
flushSync();

assert.deepEqual(values, ['6']);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

const data = {
Expand All @@ -15,9 +16,10 @@ export default test({

html: '<p>hello</p>',

async test({ assert, component, target }) {
test({ assert, component, target }) {
data.message = 'goodbye';
await component.$set({ data });
component.$set({ data });
flushSync();

assert.htmlEqual(target.innerHTML, '<p>goodbye</p>');
}
Expand Down
Loading

0 comments on commit 36ef59d

Please sign in to comment.