Skip to content

Commit 3158b6a

Browse files
author
Ahsan Hassan
committed
Fix list test
1 parent 9b0800f commit 3158b6a

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

tests/List.test.jsx

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { render, screen } from '@testing-library/react';
22
import { MemoryRouter } from 'react-router-dom';
33
import { List } from '../src/views/List';
44
import { mockShoppingListData } from '../src/mocks/__fixtures__/shoppingListData';
5-
import { useStateWithStorage, useEnsureListPath } from '../src/hooks';
5+
import {
6+
useStateWithStorage,
7+
useEnsureListPath,
8+
useUrgency,
9+
} from '../src/hooks';
610
import {
711
getDateLastPurchasedOrDateCreated,
812
getDaysFromDate,
@@ -12,20 +16,7 @@ import {
1216
vi.mock('../src/hooks', () => ({
1317
useEnsureListPath: vi.fn(),
1418
useStateWithStorage: vi.fn(),
15-
useUrgency: vi.fn(() => ({
16-
getUrgency: vi.fn((name) => {
17-
if (name === 'nutella') return 'soon';
18-
if (name === 'Cheese') return 'overdue';
19-
return 'notSoon';
20-
}),
21-
urgencyObject: {
22-
overdue: [{ name: 'nutella', id: '0T1ByXr8YJSOzujOlLMI' }],
23-
soon: [{ name: 'Cheese', id: '1MFWOWMCzDtEHQboFZfR' }],
24-
kindOfSoon: [],
25-
notSoon: [{ name: 'Jam', id: 'MnUiYUmhg8iCzX1eMxW8' }],
26-
inactive: [],
27-
},
28-
})),
19+
useUrgency: vi.fn(),
2920
}));
3021

3122
vi.mock('../src/utils', () => ({
@@ -36,6 +27,7 @@ vi.mock('../src/utils', () => ({
3627
}));
3728

3829
beforeEach(() => {
30+
vi.clearAllMocks();
3931
Object.defineProperty(window, 'localStorage', {
4032
value: {
4133
getItem: vi.fn((key) => {
@@ -52,6 +44,21 @@ beforeEach(() => {
5244

5345
vi.spyOn(window, 'alert').mockImplementation(() => {});
5446
useStateWithStorage.mockReturnValue(['/groceries']);
47+
useEnsureListPath.mockReturnValue(false);
48+
useUrgency.mockReturnValue({
49+
getUrgency: vi.fn((name) => {
50+
if (name === 'nutella') return 'soon';
51+
if (name === 'Cheese') return 'overdue';
52+
return 'notSoon';
53+
}),
54+
urgencyObject: {
55+
overdue: [{ name: 'nutella', id: '0T1ByXr8YJSOzujOlLMI' }],
56+
soon: [{ name: 'Cheese', id: '1MFWOWMCzDtEHQboFZfR' }],
57+
kindOfSoon: [],
58+
notSoon: [{ name: 'Jam', id: 'MnUiYUmhg8iCzX1eMxW8' }],
59+
inactive: [],
60+
},
61+
});
5562
getDateLastPurchasedOrDateCreated.mockReturnValue(new Date());
5663
getDaysFromDate.mockReturnValue(10);
5764
getDaysBetweenDates.mockReturnValue(5);
@@ -81,6 +88,9 @@ describe('List Component', () => {
8188
);
8289

8390
expect(screen.getByText('Welcome to groceries!')).toBeInTheDocument();
91+
expect(
92+
screen.getByText('Ready to add your first item? Start adding below!'),
93+
).toBeInTheDocument();
8494
expect(screen.getByLabelText('Item Name:')).toBeInTheDocument();
8595
expect(screen.getByLabelText('Soon')).toBeInTheDocument();
8696
expect(screen.getByLabelText('Kind of soon')).toBeInTheDocument();
@@ -89,8 +99,6 @@ describe('List Component', () => {
8999
});
90100

91101
test('triggers alert and redirects when no list path is found in localStorage', () => {
92-
window.localStorage.getItem.mockReturnValueOnce(null);
93-
94102
useEnsureListPath.mockImplementation(() => {
95103
window.alert(
96104
'It seems like you landed here without first creating a list or selecting an existing one. Please select or create a new list first. Redirecting to Home.',

0 commit comments

Comments
 (0)