Skip to content

Commit

Permalink
✅ Add basic InputText.test.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
acusti committed Jan 20, 2024
1 parent de9e340 commit 984cba7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 9 deletions.
16 changes: 14 additions & 2 deletions .pnp.cjs

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

12 changes: 11 additions & 1 deletion packages/input-text/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"ts",
"flow"
],
"scripts": {
"test": "vitest"
},
"repository": {
"type": "git",
"url": "https://github.com/acusti/uikit.git",
Expand All @@ -35,8 +38,15 @@
},
"homepage": "https://github.com/acusti/uikit/tree/main/packages/input-text#readme",
"devDependencies": {
"@testing-library/dom": "^9.3.1",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3",
"@types/react": "^18.2.45",
"typescript": "^5.3.3"
"happy-dom": "^12.10.3",
"react": "^18",
"react-dom": "^18",
"typescript": "^5.3.3",
"vitest": "^1.1.0"
},
"peerDependencies": {
"react": "^16.8 || ^17 || ^18",
Expand Down
45 changes: 45 additions & 0 deletions packages/input-text/src/InputText.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @vitest-environment happy-dom
import { cleanup, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react'; // eslint-disable-line @typescript-eslint/no-unused-vars
import { afterEach, describe, expect, it } from 'vitest';

import InputText from './InputText.js';

afterEach(cleanup);

describe('CSSValueInput.tsx', () => {
it('renders a text input with the given props.initialValue', () => {
render(<InputText initialValue="foo Bar" />);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const input = screen.getByRole('textbox') as HTMLInputElement;
expect(input.value).toBe('foo Bar');
});

it('allows value state to diverge from initialValue when updated by user but resets value state if initialValue changes', async () => {
const user = userEvent.setup();
const { rerender } = render(<InputText initialValue="foo Bar" />);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const input = screen.getByRole('textbox') as HTMLInputElement;
expect(input.value).toBe('foo Bar');
await user.type(input, '{ArrowLeft}{ArrowLeft}{ArrowLeft}{Delete}b');
expect(input.value).toBe('foo bar');
// re-render with same initialValue, value state shouldn’t change
rerender(<InputText initialValue="foo Bar" />);
expect(input.value).toBe('foo bar');
// re-render with different initialValue, value state should reset
rerender(<InputText initialValue="foo Bar " />);
expect(input.value).toBe('foo Bar ');
});

it('supports rendering multi-line inputs as a <textarea>', async () => {
const user = userEvent.setup();
const longText = 'Lorem ipsum dolor sit amet. Consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi';
render(<InputText initialValue={longText} multiLine />);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const textarea = screen.getByRole('textbox') as HTMLTextAreaElement;
expect(textarea.value).toBe(longText);
await user.type(textarea, '{Enter}New line');
expect(textarea.value).toBe(longText + '\nNew line');
});
});
19 changes: 13 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ __metadata:
version: 0.0.0-use.local
resolution: "@acusti/appsync-fetch@workspace:packages/appsync-fetch"
dependencies:
"@acusti/aws-signature-v4": "npm:^0.6.1"
"@acusti/post": "npm:^0.5.0"
"@acusti/aws-signature-v4": "npm:^0.6.2"
"@acusti/post": "npm:^0.5.1"
"@types/node": "npm:^20.10.5"
typescript: "npm:^5.3.3"
vitest: "npm:^1.1.0"
languageName: unknown
linkType: soft

"@acusti/aws-signature-v4@npm:^0.6.1, @acusti/aws-signature-v4@workspace:packages/aws-signature-v4":
"@acusti/aws-signature-v4@npm:^0.6.2, @acusti/aws-signature-v4@workspace:packages/aws-signature-v4":
version: 0.0.0-use.local
resolution: "@acusti/aws-signature-v4@workspace:packages/aws-signature-v4"
dependencies:
Expand All @@ -32,7 +32,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@acusti/css-value-input@workspace:packages/css-value-input"
dependencies:
"@acusti/css-values": "npm:^1.0.1"
"@acusti/css-values": "npm:^1.0.2"
"@acusti/input-text": "npm:^1.4.0"
"@testing-library/dom": "npm:^9.3.1"
"@testing-library/react": "npm:^14.0.0"
Expand All @@ -50,7 +50,7 @@ __metadata:
languageName: unknown
linkType: soft

"@acusti/css-values@npm:^1.0.1, @acusti/css-values@workspace:packages/css-values":
"@acusti/css-values@npm:^1.0.2, @acusti/css-values@workspace:packages/css-values":
version: 0.0.0-use.local
resolution: "@acusti/css-values@workspace:packages/css-values"
dependencies:
Expand Down Expand Up @@ -109,8 +109,15 @@ __metadata:
version: 0.0.0-use.local
resolution: "@acusti/input-text@workspace:packages/input-text"
dependencies:
"@testing-library/dom": "npm:^9.3.1"
"@testing-library/react": "npm:^14.0.0"
"@testing-library/user-event": "npm:^14.4.3"
"@types/react": "npm:^18.2.45"
happy-dom: "npm:^12.10.3"
react: "npm:^18"
react-dom: "npm:^18"
typescript: "npm:^5.3.3"
vitest: "npm:^1.1.0"
peerDependencies:
react: ^16.8 || ^17 || ^18
react-dom: ^16.8 || ^17 || ^18
Expand All @@ -126,7 +133,7 @@ __metadata:
languageName: unknown
linkType: soft

"@acusti/post@npm:^0.5.0, @acusti/post@workspace:packages/post":
"@acusti/post@npm:^0.5.1, @acusti/post@workspace:packages/post":
version: 0.0.0-use.local
resolution: "@acusti/post@workspace:packages/post"
dependencies:
Expand Down

0 comments on commit 984cba7

Please sign in to comment.