Skip to content

Commit

Permalink
fix(Input): prevent placeholder clipping, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiegyoung committed Oct 3, 2024
1 parent 01328b3 commit 9dc2b9f
Show file tree
Hide file tree
Showing 9 changed files with 4,665 additions and 3,466 deletions.
46 changes: 45 additions & 1 deletion __tests__/Input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const testInputWithContext = (
</div>
</SearchContext.Provider>
);

describe(`Input`, () => {
it(`should render correctly`, () => {
const { container } = render(<Input />);
Expand Down Expand Up @@ -44,13 +45,56 @@ describe(`Input`, () => {
expect(input.placeholder).toBe(``);
});

it('should show the full placeholder when it is a sufficient width', async () => {
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
configurable: true,
value: 250, // Set this to a value greater than 230
});

const { container } = render(testInputWithContext(``, mockSetSearchVal));

const input = container.querySelector(`input`);
if (!input) {
throw new Error(`No input found`);
}

await act(async () => {
// Trigger the event
window.dispatchEvent(new Event(`resize`));
})

expect(input.placeholder).toBe(`type here to search`);
});

it('should show the partial placeholder when it is a insufficient width', async () => {
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
configurable: true,
value: 200, // Set this to a value greater than 230
});

const { container } = render(testInputWithContext(``, mockSetSearchVal));

const input = container.querySelector(`input`);
if (!input) {
throw new Error(`No input found`);
}

await act(async () => {
// Trigger the event
window.dispatchEvent(new Event(`resize`));
})

expect(input.placeholder).toBe(`search tiles`);
});

it(`should shrink on blur`, () => {
const { container } = render(testInputWithContext(``, mockSetSearchVal));

const input = container.querySelector(`input`);
if (!input) {
throw new Error(`No input found`);
}

act(() => {
fireEvent.focus(input);
});
Expand All @@ -59,7 +103,7 @@ describe(`Input`, () => {
input.value = ``;
fireEvent.blur(input);
});
expect(input.placeholder).toBe(`type here to search`);
expect(input.placeholder).toBe(`search tiles`);
});

it(`should update the search value on change`, () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/__snapshots__/Input.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Input should render correctly 1`] = `
<div>
<input
class="input "
placeholder="type here to search"
placeholder="search tiles"
type="text"
value=""
/>
Expand Down
4 changes: 2 additions & 2 deletions __tests__/__snapshots__/ListOperations.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports[`ListOperations should render correctly with the grid format 1`] = `
>
<input
class="input "
placeholder="type here to search"
placeholder="search tiles"
type="text"
value=""
/>
Expand Down Expand Up @@ -71,7 +71,7 @@ exports[`ListOperations should render correctly with the list format 1`] = `
>
<input
class="input "
placeholder="type here to search"
placeholder="search tiles"
type="text"
value=""
/>
Expand Down
2 changes: 1 addition & 1 deletion __tests__/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ exports[`Home should search correctly 1`] = `
>
<input
class="input "
placeholder="type here to search"
placeholder="search tiles"
type="text"
value="abyssal sire"
/>
Expand Down
2 changes: 1 addition & 1 deletion __tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe(`Home`, () => {
const { container, getByPlaceholderText } = render(
<Home tileData={data} />,
);
const input = getByPlaceholderText(`type here to search`);
const input = getByPlaceholderText(`search tiles`);
act(() => {
fireEvent.change(input, { target: { value: `abyssal sire` } });
});
Expand Down
Loading

0 comments on commit 9dc2b9f

Please sign in to comment.