Skip to content

Commit 542a560

Browse files
committed
Remove usage of faker in tests
1 parent 3d0ccfa commit 542a560

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

packages/ra-ui-materialui/src/button/PrevNextButtons.spec.tsx

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as React from 'react';
22
import { fireEvent, render, screen } from '@testing-library/react';
3-
import { faker } from '@faker-js/faker';
43
import fakeRestDataProvider from 'ra-data-fakerest';
54

65
import {
@@ -23,15 +22,15 @@ describe('<PrevNextButtons />', () => {
2322

2423
it('should render the current record position according to the clicked item in the list', async () => {
2524
render(<Basic />);
26-
const tr = await screen.findByText('Deja');
25+
const tr = await screen.findByText('first_name_3');
2726
fireEvent.click(tr);
2827
await screen.findByRole('navigation');
2928
expect(screen.getByText('4 / 900')).toBeDefined();
3029
});
3130

3231
it('should render previous button as disabled if there is no previous record', async () => {
3332
render(<Basic />);
34-
const tr = await screen.findByText('Maurine');
33+
const tr = await screen.findByText('first_name_0');
3534
fireEvent.click(tr);
3635
await screen.findByRole('navigation');
3736
const previousButton = screen.getByLabelText('Go to previous page');
@@ -43,7 +42,7 @@ describe('<PrevNextButtons />', () => {
4342
render(<Basic />);
4443
const lastPage = await screen.findByText('90');
4544
fireEvent.click(lastPage);
46-
const tr = await screen.findByText('Maxwell');
45+
const tr = await screen.findByText('first_name_899');
4746
fireEvent.click(tr);
4847
await screen.findByRole('navigation');
4948
const nextButton = screen.getByLabelText('Go to next page');
@@ -54,16 +53,17 @@ describe('<PrevNextButtons />', () => {
5453
it('should render a total based on query filter', async () => {
5554
render(<WithQueryFilter />);
5655
const input = await screen.findByLabelText('Search');
57-
fireEvent.change(input, { target: { value: 'east' } });
58-
const item = await screen.findByText('217');
56+
fireEvent.change(input, { target: { value: 'city_0' } });
57+
await screen.findByText('1-10 of 50');
58+
const item = await screen.findByText('first_name_9');
5959
fireEvent.click(item);
6060
await screen.findByRole('navigation');
61-
await screen.findByText('10 / 57');
61+
await screen.findByText('10 / 50');
6262
});
6363

6464
it('should link to the edit view by default', async () => {
6565
render(<Basic />);
66-
const row = await screen.findByText('Deja');
66+
const row = await screen.findByText('first_name_0');
6767
fireEvent.click(row);
6868
fireEvent.click(await screen.findByLabelText('Edit'));
6969
const next = await screen.findByLabelText('Go to next page');
@@ -90,7 +90,7 @@ describe('<PrevNextButtons />', () => {
9090
describe('linkType', () => {
9191
it('should link to the show view when linkType is show', async () => {
9292
render(<Basic />);
93-
const row = await screen.findByText('Deja');
93+
const row = await screen.findByText('first_name_0');
9494
fireEvent.click(row);
9595
const next = await screen.findByLabelText('Go to next page');
9696
fireEvent.click(next);
@@ -102,10 +102,10 @@ describe('<PrevNextButtons />', () => {
102102
describe('filter', () => {
103103
it('should render a total based on filter', async () => {
104104
render(<WithFilter />);
105-
const item = await screen.findByText('822');
105+
const item = await screen.findByText('first_name_5');
106106
fireEvent.click(item);
107107
await screen.findByRole('navigation');
108-
expect(screen.getByText('1 / 5')).toBeDefined();
108+
expect(screen.getByText('5 / 50')).toBeDefined();
109109
});
110110
});
111111

@@ -119,19 +119,16 @@ describe('<PrevNextButtons />', () => {
119119
it('should limit the number of items fetched from the data provider', async () => {
120120
const data = {
121121
customers: Array.from(Array(900).keys()).map(id => {
122-
const first_name = faker.person.firstName();
123-
const last_name = faker.person.lastName();
124-
const email = faker.internet.email({
125-
firstName: first_name,
126-
lastName: last_name,
127-
});
122+
const first_name = `first_name_${id}`;
123+
const last_name = `last_name_${id}`;
124+
const email = `first_name_${id}.last_name_${id}@example.com`;
128125

129126
return {
130127
id,
131128
first_name,
132129
last_name,
133130
email,
134-
city: faker.location.city(),
131+
city: `city_${id}`,
135132
};
136133
}),
137134
};
@@ -154,49 +151,49 @@ describe('<PrevNextButtons />', () => {
154151
describe('pagination', () => {
155152
it('should compute the index correctly when opening first record of page 2', async () => {
156153
render(<Basic />);
157-
await screen.findByText('Deja');
154+
await screen.findByText('first_name_1');
158155
fireEvent.click(await screen.findByLabelText('Go to page 2'));
159-
const tr = await screen.findByText('Hamill');
156+
const tr = await screen.findByText('first_name_10');
160157
fireEvent.click(tr);
161158
await screen.findByText('11 / 900');
162159
});
163160
it('should compute the index correctly when opening second record of page 2', async () => {
164161
render(<Basic />);
165-
await screen.findByText('Deja');
162+
await screen.findByText('first_name_1');
166163
fireEvent.click(await screen.findByLabelText('Go to page 2'));
167-
const tr = await screen.findByText('Kub');
164+
const tr = await screen.findByText('first_name_11');
168165
fireEvent.click(tr);
169166
await screen.findByText('12 / 900');
170167
});
171168
it('should compute the index correctly when opening last record of page 2', async () => {
172169
render(<Basic />);
173-
await screen.findByText('Deja');
170+
await screen.findByText('first_name_1');
174171
fireEvent.click(await screen.findByLabelText('Go to page 2'));
175-
const tr = await screen.findByText('Langworth');
172+
const tr = await screen.findByText('first_name_19');
176173
fireEvent.click(tr);
177174
await screen.findByText('20 / 900');
178175
});
179176
it('should compute the index correctly when opening first record of page 3', async () => {
180177
render(<Basic />);
181-
await screen.findByText('Deja');
178+
await screen.findByText('first_name_1');
182179
fireEvent.click(await screen.findByLabelText('Go to page 3'));
183-
const tr = await screen.findByText('Spinka');
180+
const tr = await screen.findByText('first_name_20');
184181
fireEvent.click(tr);
185182
await screen.findByText('21 / 900');
186183
});
187184
it('should compute the index correctly when opening second record of page 3', async () => {
188185
render(<Basic />);
189-
await screen.findByText('Deja');
186+
await screen.findByText('first_name_5');
190187
fireEvent.click(await screen.findByLabelText('Go to page 3'));
191-
const tr = await screen.findByText('Lowe');
188+
const tr = await screen.findByText('first_name_21');
192189
fireEvent.click(tr);
193190
await screen.findByText('22 / 900');
194191
});
195192
it('should compute the index correctly when opening last record of page 3', async () => {
196193
render(<Basic />);
197-
await screen.findByText('Deja');
194+
await screen.findByText('first_name_5');
198195
fireEvent.click(await screen.findByLabelText('Go to page 3'));
199-
const tr = await screen.findByText('Grimes');
196+
const tr = await screen.findByText('first_name_29');
200197
fireEvent.click(tr);
201198
await screen.findByText('30 / 900');
202199
});

packages/ra-ui-materialui/src/button/PrevNextButtons.stories.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
} from 'ra-core';
1010
import englishMessages from 'ra-language-english';
1111
import polyglotI18nProvider from 'ra-i18n-polyglot';
12-
import { faker } from '@faker-js/faker';
1312
import { QueryClient } from '@tanstack/react-query';
1413

1514
import {
@@ -32,23 +31,26 @@ export default { title: 'ra-ui-materialui/button/PrevNextButtons' };
3231

3332
const i18nProvider = polyglotI18nProvider(() => englishMessages, 'en');
3433

35-
faker.seed(123); // we want consistent results
34+
let cityCounter = 0;
35+
let city = `city_${cityCounter}`;
3636

3737
const data = {
38-
customers: Array.from(Array(900).keys()).map(id => {
39-
const first_name = faker.person.firstName();
40-
const last_name = faker.person.lastName();
41-
const email = faker.internet.email({
42-
firstName: first_name,
43-
lastName: last_name,
44-
});
38+
customers: Array.from(Array(900).keys()).map((id, index) => {
39+
const first_name = `first_name_${id}`;
40+
const last_name = `last_name_${id}`;
41+
const email = `email_${id}@example.com`;
42+
// Increment city every 50 records
43+
if (index % 50 === 0 && index !== 0) {
44+
cityCounter += 1;
45+
city = `city_${cityCounter}`;
46+
}
4547

4648
return {
4749
id,
4850
first_name,
4951
last_name,
5052
email,
51-
city: faker.location.city(),
53+
city,
5254
};
5355
}),
5456
};
@@ -162,7 +164,7 @@ export const WithFilter = () => (
162164
name="customers"
163165
list={
164166
<ListGuesser
165-
filter={{ city_q: 'East A' }}
167+
filter={{ city_q: 'city_0' }}
166168
sort={{ field: 'first_name', order: 'DESC' }}
167169
/>
168170
}
@@ -175,7 +177,7 @@ export const WithFilter = () => (
175177
field: 'first_name',
176178
order: 'DESC',
177179
}}
178-
filter={{ city_q: 'East A' }}
180+
filter={{ city_q: 'city_0' }}
179181
/>
180182
<ShowButton />
181183
</MyTopToolbar>
@@ -192,7 +194,7 @@ export const WithFilter = () => (
192194
field: 'first_name',
193195
order: 'DESC',
194196
}}
195-
filter={{ city_q: 'East A' }}
197+
filter={{ city_q: 'city_0' }}
196198
/>
197199
<EditButton />
198200
</MyTopToolbar>

0 commit comments

Comments
 (0)