Skip to content

Commit

Permalink
Add Preact component tests
Browse files Browse the repository at this point in the history
Add a first step for a Preact component in h.

For now this isn't a very useful test (and the component being tested
doesn't have any dynamic behaviour to test), but it suffices to ensure
that the necessary test dependencies are setup (e.g. Enzyme).
  • Loading branch information
seanh committed Jul 11, 2024
1 parent f03401e commit d209a92
Show file tree
Hide file tree
Showing 6 changed files with 466 additions and 47 deletions.
26 changes: 22 additions & 4 deletions h/static/scripts/group-forms/components/CreateGroupForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ function Star() {
return <span className="text-brand">*</span>;
}

function CharacterCounter({ value, limit }: { value: number; limit: number }) {
function CharacterCounter({
value,
limit,
testid,
}: {
value: number;
limit: number;
testid: string;
}) {
return (
<div className="flex">
<div className="grow" />
{value}/{limit}
<span data-testid={testid}>
{value}/{limit}
</span>
</div>
);
}
Expand Down Expand Up @@ -46,13 +56,21 @@ export default function CreateGroupForm() {
<div className="mb-4">
<Label htmlFor={nameId} text="Name" required />
<Input id={nameId} autofocus autocomplete="off" required />
<CharacterCounter value={0} limit={25} />
<CharacterCounter
value={0}
limit={25}
testid="character-counter-name"
/>
</div>

<div className="mb-4">
<Label htmlFor={descriptionId} text="Description" />
<Textarea id={descriptionId} classes="h-24" />
<CharacterCounter value={0} limit={250} />
<CharacterCounter
value={0}
limit={250}
testid="character-counter-description"
/>
</div>

<div className="flex">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { mount } from 'enzyme';

import CreateGroupForm from '../CreateGroupForm';

describe('CreateGroupForm', () => {
const createWrapper = () => mount(<CreateGroupForm />);

it('renders', async () => {
const wrapper = createWrapper();
const characterCounterName = wrapper.find(
'[data-testid="character-counter-name"]',
);
const characterCounterDescription = wrapper.find(
'[data-testid="character-counter-description"]',
);

assert(characterCounterName.text() === '0/25');
assert(characterCounterDescription.text() === '0/250');
});
});
5 changes: 5 additions & 0 deletions h/static/scripts/tests/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { assert } from 'chai';
import { configure } from 'enzyme';
import { Adapter } from 'enzyme-adapter-preact-pure';
import sinon from 'sinon';

// Expose the sinon assertions.
Expand All @@ -8,3 +10,6 @@ sinon.assert.expose(assert, { prefix: null });
// karma-sinon plugins
globalThis.assert = assert;
globalThis.sinon = sinon;

// Configure Enzyme for UI tests.
configure({ adapter: new Adapter() });
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
"babel-plugin-mockable-imports": "^2.0.1",
"chai": "^5.1.1",
"diff": "^5.2.0",
"enzyme": "^3.11.0",
"enzyme-adapter-preact-pure": "^4.1.0",
"eslint": "^8.57.0",
"eslint-config-hypothesis": "2.6",
"eslint-plugin-mocha": "^10.4.3",
Expand Down
21 changes: 20 additions & 1 deletion rollup-tests.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export default {
sourcemap: true,
},
treeshake: false,
// Suppress a warning (https://rollupjs.org/guide/en/#error-this-is-undefined)
// due to https://github.com/babel/babel/issues/9149.
//
// Any code string other than "undefined" which evaluates to `undefined` will work here.
context: 'void(0)',
plugins: [
nodeResolve({
browser: true,
Expand All @@ -22,11 +27,25 @@ export default {
// configuration for the `virtual` plugin above.
preferBuiltins: false,
}),
commonjs(),
commonjs({
include: 'node_modules/**',
}),
babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**',
extensions: ['.js', '.ts', '.tsx'],
presets: [
[
'@babel/preset-react',
{
// Turn off the `development` setting in tests to prevent warnings
// about `this`. See https://github.com/babel/babel/issues/9149.
development: false,
runtime: 'automatic',
importSource: 'preact',
},
],
],
plugins: ['mockable-imports'],
}),
],
Expand Down
Loading

0 comments on commit d209a92

Please sign in to comment.