-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91d45fa
commit 2793742
Showing
3 changed files
with
99 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,94 @@ | ||
import { | ||
describe, | ||
it, | ||
expect, | ||
beforeAll, | ||
afterAll, | ||
vi, | ||
type SpyInstance | ||
} from 'vitest' | ||
import { render, screen } from '@testing-library/react' | ||
import * as nanostores from '@nanostores/react' | ||
import Location from '.' | ||
|
||
const mockData = { | ||
now: { | ||
country: 'USA', | ||
country_code: 'US', | ||
city: 'New York' | ||
}, | ||
next: { | ||
country: 'Canada', | ||
country_code: 'CA', | ||
city: 'Toronto', | ||
date_start: '2023-10-05' | ||
} | ||
} | ||
|
||
describe('Location component', () => { | ||
let useStoreSpy: SpyInstance | ||
|
||
beforeAll(() => { | ||
vi.mock('@nanostores/react') | ||
useStoreSpy = vi.spyOn(nanostores, 'useStore') | ||
}) | ||
|
||
afterAll(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
it('renders the location items correctly', () => { | ||
useStoreSpy.mockImplementationOnce(() => ({ | ||
data: mockData, | ||
loading: false, | ||
error: null | ||
})) | ||
|
||
render(<Location />) | ||
|
||
expect(screen.getByLabelText('USA')).toBeInTheDocument() | ||
expect(screen.getByText('New York')).toBeInTheDocument() | ||
expect(screen.getByLabelText('Canada')).toBeInTheDocument() | ||
expect(screen.getByText('Toronto')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders the loading indicator', () => { | ||
useStoreSpy.mockImplementationOnce(() => ({ | ||
data: null, | ||
loading: true, | ||
error: null | ||
})) | ||
|
||
render(<Location />) | ||
|
||
expect(screen.getByText('...')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders empty when there is no data', () => { | ||
useStoreSpy.mockImplementationOnce(() => ({ | ||
data: null, | ||
loading: false, | ||
error: null | ||
})) | ||
|
||
render(<Location />) | ||
|
||
expect(screen.queryByLabelText('Location')).toBeEmptyDOMElement() | ||
}) | ||
|
||
it('renders nothing and logs error when error is encountered', () => { | ||
const consoleErrorSpy = vi.spyOn(console, 'error') | ||
useStoreSpy.mockImplementationOnce(() => ({ | ||
data: null, | ||
loading: false, | ||
error: 'Error' | ||
})) | ||
|
||
render(<Location />) | ||
|
||
expect(screen.queryByLabelText('Location')).not.toBeInTheDocument() | ||
expect(consoleErrorSpy).toHaveBeenCalledWith( | ||
'Failed to fetch location: Error' | ||
) | ||
}) | ||
}) |
This file contains 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