Skip to content

Commit

Permalink
add tests for component
Browse files Browse the repository at this point in the history
  • Loading branch information
sebkolind committed May 17, 2024
1 parent ed5562e commit 94d7764
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 9 deletions.
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const config: Config = {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 80,
branches: 75,
functions: 50,
lines: 50,
statements: 50,
Expand Down
146 changes: 139 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"main": "dist/main.js",
"module": "dist/module.js",
"types": "dist/main.d.ts",
"files": ["dist"],
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+ssh://[email protected]/tentjs/tent.git"
Expand All @@ -24,6 +26,7 @@
"devDependencies": {
"@parcel/packager-ts": "^2.10.3",
"@parcel/transformer-typescript-types": "^2.10.3",
"@testing-library/dom": "^10.1.0",
"@types/jest": "^29.5.12",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
Expand Down
50 changes: 50 additions & 0 deletions src/__tests__/component.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { mount } from '../main';
import { tags } from '../tags';
import { type Component } from '../types';
import { getByText, getByTestId, fireEvent } from '@testing-library/dom';

const { div, p, button } = tags;

const Counter: Component<{ count: number }> = {
state: { count: 0 },
view: ({ state }) =>
div(
[
p(`Count: ${state.count}`),
button('Increment', {
onclick: () => state.count++,
className: `${state.count > 0 ? 'positive' : 'negative'}`,
autofocus: state.count === 0,
['data-testid']: 'increment',
}),
],
{ ['data-testid']: 'counter' },
),
};

describe('components', () => {
test('simple interactivity', () => {
mount(document.body, Counter);

const el = getByTestId(document.body, 'counter');
const btn = getByTestId(document.body, 'increment');

expect(getByText(el, /Count: 0/));
expect(btn.className).toBe('negative');
expect(btn.hasAttribute('autofocus')).toBe(true);

fireEvent.click(btn);

expect(getByText(el, /Count: 1/));
expect(btn.className).toBe('positive');
expect(btn.hasAttribute('autofocus')).toBe(false);
});

test('mounted', () => {
const mounted = jest.fn();

mount(document.body, { ...Counter, mounted });

expect(mounted).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 94d7764

Please sign in to comment.