Skip to content

Commit

Permalink
test: Add expect.assertions to all unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmilton committed Mar 20, 2024
1 parent 68a5950 commit 5e8c4b3
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/unit/browser-compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('h', () => {
afterEach(cleanup);

test('renders basic template', () => {
expect.assertions(1);
const view = h(`
<ul>
<li>A</li>
Expand All @@ -20,6 +21,7 @@ describe('h', () => {
});

test('renders basic template with messy whitespace', () => {
expect.assertions(1);
const view = h(`
<ul>
<li \f\n\r\t\v\u0020\u00A0\u1680\u2000\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF >A</li>
Expand All @@ -35,6 +37,7 @@ describe('h', () => {
});

test('renders SVG template', () => {
expect.assertions(2);
const view = h(`
<svg>
<circle cx=10 cy='10' r="10" />
Expand All @@ -48,6 +51,7 @@ describe('h', () => {
});

test('returns root element', () => {
expect.assertions(3);
const view = h(`
<ul id=root>
<li>A</li>
Expand All @@ -62,6 +66,7 @@ describe('h', () => {
});

test('removes refs in template from output DOM', () => {
expect.assertions(1);
const view = h(`
<ul @list>
<li @item-one>A</li>
Expand All @@ -81,6 +86,7 @@ describe('html', () => {
afterEach(cleanup);

test('renders basic template', () => {
expect.assertions(1);
const view = html`
<ul>
<li>A</li>
Expand All @@ -95,6 +101,7 @@ describe('html', () => {

describe('collect', () => {
test('collects all refs', () => {
expect.assertions(39);
const view = h(`
<div @a>
<header @b>
Expand Down Expand Up @@ -164,6 +171,7 @@ describe('collect', () => {
});

test('collects ref at start of element attributes', () => {
expect.assertions(4);
const view = h(`
<div>
<input @search id=search name=q class="input search" type=search minlength=2 maxlength=40 placeholder="Search..." autofocus autocomplete=off />
Expand All @@ -177,6 +185,7 @@ describe('collect', () => {
});

test('collects ref at end of element attributes', () => {
expect.assertions(4);
const view = h(`
<div>
<input id=search name=q class="input search" type=search minlength=2 maxlength=40 placeholder="Search..." autofocus autocomplete=off @search />
Expand All @@ -190,6 +199,7 @@ describe('collect', () => {
});

test('collects ref in middle of element attributes', () => {
expect.assertions(4);
const view = h(`
<div>
<input id=search name=q class="input search" type=search minlength=2 @search maxlength=40 placeholder="Search..." autofocus autocomplete=off />
Expand Down
14 changes: 14 additions & 0 deletions test/unit/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ declare global {

describe('setupSyntheticEvent', () => {
test('is a function', () => {
expect.assertions(1);
expect(setupSyntheticEvent).toBeInstanceOf(Function);
});

test('expects 1 parameter', () => {
expect.assertions(1);
expect(setupSyntheticEvent).toHaveLength(1);
});

test('returns undefined', () => {
expect.assertions(1);
expect(setupSyntheticEvent('abort')).toBeUndefined();
});

describe('rendered', () => {
afterEach(cleanup);

test('calls synthetic click handler on click event', () => {
expect.assertions(1);
const button = document.createElement('button');
const callback = mock(() => {});
button.__click = callback;
Expand All @@ -39,6 +43,7 @@ describe('setupSyntheticEvent', () => {
});

test('propagates click event from deeply nested element', () => {
expect.assertions(1);
const button = document.createElement('button');
const div = document.createElement('div');
const span = document.createElement('span');
Expand All @@ -58,6 +63,7 @@ describe('setupSyntheticEvent', () => {
});

test('propagates up to document body', () => {
expect.assertions(1);
const button = document.createElement('button');
const callback = mock(() => {});
document.body.__click = callback;
Expand All @@ -71,6 +77,7 @@ describe('setupSyntheticEvent', () => {
});

test('no longer propagates click event once handled', () => {
expect.assertions(1);
const div1 = document.createElement('div');
const div2 = document.createElement('div');
const callback = mock(() => {});
Expand All @@ -85,6 +92,7 @@ describe('setupSyntheticEvent', () => {
});

test('does not call handler if synthetic event is not setup', () => {
expect.assertions(1);
const button = document.createElement('button');
const callback = mock(() => {});
button.__click = callback;
Expand All @@ -94,6 +102,7 @@ describe('setupSyntheticEvent', () => {
});

test('does not call handler if event originates from another DOM tree branch', () => {
expect.assertions(2);
const div = document.createElement('div');
const button1 = document.createElement('button');
const button2 = document.createElement('button');
Expand All @@ -112,6 +121,7 @@ describe('setupSyntheticEvent', () => {
});

test('only registers synthetic click handler once', () => {
expect.assertions(1);
const button = document.createElement('button');
const callback = mock(() => {});
button.__click = callback;
Expand All @@ -127,21 +137,25 @@ describe('setupSyntheticEvent', () => {

describe('deleteSyntheticEvent', () => {
test('is a function', () => {
expect.assertions(1);
expect(deleteSyntheticEvent).toBeInstanceOf(Function);
});

test('expects 1 parameter', () => {
expect.assertions(1);
expect(deleteSyntheticEvent).toHaveLength(1);
});

test('returns undefined', () => {
expect.assertions(1);
expect(deleteSyntheticEvent('abort')).toBeUndefined();
});

describe('rendered', () => {
afterEach(cleanup);

test('does not call synthetic click handler after delete', () => {
expect.assertions(2);
const button = document.createElement('button');
const callback = mock(() => {});
button.__click = callback;
Expand Down
12 changes: 12 additions & 0 deletions test/unit/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ describe('browser', () => {

for (const [name, type] of publicExports) {
test(`exports public "${name}" ${type.name}`, () => {
expect.assertions(2);
expect(browserExports).toHaveProperty(name);
expect(browserExports[name]).toBeInstanceOf(type);
});
}

test('does not export any private internals', () => {
expect.assertions(publicExports.length + 1);
const publicExportNames = publicExports.map((x) => x[0]);
for (const name in browserExports) {
expect(publicExportNames).toContain(name);
Expand All @@ -41,6 +43,7 @@ describe('browser', () => {
});

test('has no default export', () => {
expect.assertions(1);
expect(browserExports).not.toHaveProperty('default');
});
});
Expand All @@ -63,12 +66,14 @@ describe('index', () => {

for (const [name, type] of publicExports) {
test(`exports public "${name}" ${type.name}`, () => {
expect.assertions(2);
expect(indexExports).toHaveProperty(name);
expect(indexExports[name]).toBeInstanceOf(type);
});
}

test('does not export any private internals', () => {
expect.assertions(publicExports.length + 1);
const publicExportNames = publicExports.map((x) => x[0]);
for (const name in indexExports) {
expect(publicExportNames).toContain(name);
Expand All @@ -77,6 +82,7 @@ describe('index', () => {
});

test('has no default export', () => {
expect.assertions(1);
expect(indexExports).not.toHaveProperty('default');
});
});
Expand All @@ -86,12 +92,14 @@ describe('macro', () => {

for (const [name, type] of publicExports) {
test(`exports public "${name}" ${type.name}`, () => {
expect.assertions(2);
expect(macroExports).toHaveProperty(name);
expect(macroExports[name]).toBeInstanceOf(type);
});
}

test('does not export any private internals', () => {
expect.assertions(publicExports.length + 1);
const publicExportNames = publicExports.map((x) => x[0]);
for (const name in macroExports) {
expect(publicExportNames).toContain(name);
Expand All @@ -100,6 +108,7 @@ describe('macro', () => {
});

test('has no default export', () => {
expect.assertions(1);
expect(macroExports).not.toHaveProperty('default');
});
});
Expand All @@ -113,11 +122,13 @@ const reconsilers = [
for (const [reconsiler, exports] of reconsilers) {
describe(`reconcile/${reconsiler}`, () => {
test('exports public "reconcile" Function', () => {
expect.assertions(2);
expect(exports).toHaveProperty('reconcile');
expect(exports.reconcile).toBeInstanceOf(Function);
});

test('does not export any private internals', () => {
expect.assertions(2);
const publicExportNames = ['reconcile'];
for (const name in exports) {
expect(publicExportNames).toContain(name);
Expand All @@ -126,6 +137,7 @@ for (const [reconsiler, exports] of reconsilers) {
});

test('has no default export', () => {
expect.assertions(1);
expect(exports).not.toHaveProperty('default');
});
});
Expand Down
Loading

0 comments on commit 5e8c4b3

Please sign in to comment.