-
Notifications
You must be signed in to change notification settings - Fork 4
Fix | SummitDropdown | Type Error - null is not an object (evaluating 'this.state.summitValue.value') #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matiasperrone-exo
wants to merge
8
commits into
main
Choose a base branch
from
fix/type-error---null-is-not-an-object-evaluating-this.state.summitvalue.value
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−7
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5192916
fix: Protect against this.state.summitValue being null, shouldn't exc…
matiasperrone bb8ec65
fix: SummitDropdown | Type Error - null is not an object (evaluating …
matiasperrone 68d74ec
chore: Add AI recommended change (non critical)
matiasperrone bcb6dda
chore: Add requested changes
matiasperrone-exo 05a1ee8
chore: Change test to mimic component behavior
matiasperrone-exo 7b39c70
chore: Add enzyme and enzyme-adapter-react-16 as pkg list for testing
matiasperrone-exo 69abb45
chore: move to enzyme to dev dependencies
matiasperrone-exo 73c3f05
chore: migrate from "enzyme" to "@testing-library/react"
matiasperrone-exo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,6 +190,5 @@ | |
| "console": {} | ||
| }, | ||
| "testEnvironment": "jsdom" | ||
| }, | ||
| "dependencies": {} | ||
| } | ||
| } | ||
120 changes: 120 additions & 0 deletions
120
src/components/summit-dropdown/__tests__/summit-dropdown.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /** | ||
| * @jest-environment jsdom | ||
| */ | ||
| import React from 'react'; | ||
| import "@testing-library/jest-dom"; | ||
| import { render, screen, act } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import SummitDropdown from '..'; | ||
|
|
||
| jest.mock('i18n-react/dist/i18n-react', () => ({ | ||
| __esModule: true, | ||
| default: { translate: (key) => key }, | ||
| })); | ||
|
|
||
| const summits = [ | ||
| { id: 1, name: 'Summit A', start_date: 1000 }, | ||
| { id: 2, name: 'Summit B', start_date: 2000 }, | ||
| ]; | ||
|
|
||
| const defaultProps = { | ||
| summits, | ||
| actionLabel: 'Go', | ||
| actionClass: '', | ||
| onClick: jest.fn(), | ||
| }; | ||
|
|
||
| function renderComponent(props = {}) { | ||
| return render(<SummitDropdown {...defaultProps} {...props} />); | ||
| } | ||
|
|
||
| describe('SummitDropdown summitValue state', () => { | ||
| beforeEach(() => { | ||
| defaultProps.onClick.mockClear(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| test('summitValue is null on initial render', () => { | ||
| renderComponent(); | ||
| expect(screen.getByRole('button', { name: 'Go' })).toBeDisabled(); | ||
| }); | ||
|
|
||
| test('handleChange sets summitValue when given a valid object', async () => { | ||
| const user = userEvent.setup(); | ||
| renderComponent(); | ||
|
|
||
| await user.click(screen.getByRole('textbox')); | ||
| await user.click(screen.getByText('Summit A')); | ||
|
|
||
| expect(screen.getByRole('button', { name: 'Go' })).toBeEnabled(); | ||
| }); | ||
|
|
||
| test('handleChange does not set summitValue when given a non-object', () => { | ||
| const ref = React.createRef(); | ||
| render(<SummitDropdown {...defaultProps} ref={ref} />); | ||
|
|
||
| const option = { label: 'Summit A', value: 1 }; | ||
| const invalidOption = 'not-an-object'; | ||
|
|
||
| act(() => { ref.current.handleChange(invalidOption); }); | ||
| expect(ref.current.state.summitValue).toBeNull(); | ||
|
|
||
| act(() => { ref.current.handleChange(option); }); | ||
| expect(ref.current.state.summitValue).toEqual(option); | ||
|
|
||
| act(() => { ref.current.handleChange(invalidOption); }); | ||
| expect(ref.current.state.summitValue).toEqual(option); | ||
| }); | ||
|
|
||
| test('handleChange does not set summitValue when given null', () => { | ||
| const ref = React.createRef(); | ||
| render(<SummitDropdown {...defaultProps} ref={ref} />); | ||
|
|
||
| const option = { label: 'Summit A', value: 1 }; | ||
|
|
||
| act(() => { ref.current.handleChange(null); }); | ||
| expect(ref.current.state.summitValue).toBeNull(); | ||
|
|
||
| act(() => { ref.current.handleChange(option); }); | ||
| expect(ref.current.state.summitValue).toEqual(option); | ||
|
|
||
| act(() => { ref.current.handleChange(null); }); | ||
| expect(ref.current.state.summitValue).toEqual(option); | ||
| }); | ||
|
|
||
| test('handleClick does not call onClick when summitValue is null', async () => { | ||
| const user = userEvent.setup(); | ||
| renderComponent(); | ||
|
|
||
| // button is disabled — click is a no-op, onClick must NOT be called | ||
| await user.click(screen.getByRole('button', { name: 'Go' })); | ||
|
|
||
| expect(defaultProps.onClick).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('handleClick calls onClick with summit id when summitValue is set', async () => { | ||
| const user = userEvent.setup(); | ||
| renderComponent(); | ||
|
|
||
| await user.click(screen.getByRole('textbox')); | ||
| await user.click(screen.getByText('Summit A')); | ||
| await user.click(screen.getByRole('button', { name: 'Go' })); | ||
|
|
||
| expect(defaultProps.onClick).toHaveBeenCalledWith(1); | ||
| }); | ||
|
|
||
| test('button is disabled when summitValue is null', () => { | ||
| renderComponent(); | ||
| expect(screen.getByRole('button', { name: 'Go' })).toBeDisabled(); | ||
| }); | ||
|
|
||
| test('button is enabled after selecting a summit', async () => { | ||
| const user = userEvent.setup(); | ||
| renderComponent(); | ||
|
|
||
| await user.click(screen.getByRole('textbox')); | ||
| await user.click(screen.getByText('Summit A')); | ||
|
|
||
| expect(screen.getByRole('button', { name: 'Go' })).toBeEnabled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.