Skip to content

Commit cf3ec42

Browse files
committed
add test
1 parent 94fcb59 commit cf3ec42

File tree

3 files changed

+52
-120
lines changed

3 files changed

+52
-120
lines changed

packages/@react-spectrum/s2/stories/TableView.stories.tsx

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import {action} from '@storybook/addon-actions';
1414
import {
1515
ActionButton,
16-
ActionButtonGroup,
1716
Cell,
1817
Column,
1918
Content,
@@ -33,7 +32,7 @@ import {categorizeArgTypes} from './utils';
3332
import Filter from '../s2wf-icons/S2_Icon_Filter_20_N.svg';
3433
import FolderOpen from '../spectrum-illustrations/linear/FolderOpen';
3534
import type {Meta, StoryObj} from '@storybook/react';
36-
import {ReactElement, useEffect, useState} from 'react';
35+
import {ReactElement, useState} from 'react';
3736
import {SortDescriptor} from 'react-aria-components';
3837
import {style} from '../style/spectrum-theme' with {type: 'macro'};
3938
import {useAsyncList} from '@react-stately/data';
@@ -1389,62 +1388,3 @@ const ResizableTable = () => {
13891388
}
13901389
}
13911390
};
1392-
1393-
1394-
export const Test = {
1395-
render: () => {
1396-
return <SWTable />;
1397-
}
1398-
};
1399-
1400-
function SWTable() {
1401-
const [film, setFilm] = useState(undefined);
1402-
1403-
interface Item {
1404-
id: string,
1405-
title: string
1406-
}
1407-
1408-
let list = useAsyncList<Item>({
1409-
async load({signal, cursor}) {
1410-
if (cursor) {
1411-
cursor = cursor.replace(/^http:\/\//i, 'https://');
1412-
}
1413-
let items = [];
1414-
await new Promise(resolve => setTimeout(resolve, 1500));
1415-
let res = await fetch(cursor || `https://swapi.py4e.com/api/people/?search&film=${film}`, {signal});
1416-
let json = await res.json();
1417-
items = json.results.map((element) => ({title: element.name}));
1418-
1419-
return {
1420-
items: items,
1421-
cursor: json.next
1422-
};
1423-
}
1424-
});
1425-
1426-
useEffect(() => {
1427-
list.reload();
1428-
}, [film]);
1429-
1430-
return (
1431-
<div>
1432-
<ActionButtonGroup aria-label="group">
1433-
<ActionButton onPress={() => setFilm(undefined)}>All</ActionButton>
1434-
<ActionButton onPress={() => setFilm('https://swapi.py4e.com/api/films/1/')}>A New Hope</ActionButton>
1435-
</ActionButtonGroup>
1436-
<TableView aria-label="Characters" loadingState={list.loadingState} onLoadMore={list.loadMore} styles={style({height: 200, width: 400})}>
1437-
<TableHeader>
1438-
<Column isRowHeader>Name</Column>
1439-
</TableHeader>
1440-
<TableBody items={list.items}>
1441-
{
1442-
item => (<Row id={item.title}>
1443-
<Cell>{item.title}</Cell>
1444-
</Row>)
1445-
}
1446-
</TableBody>
1447-
</TableView>
1448-
</div>
1449-
);
1450-
}

packages/@react-spectrum/table/stories/Table.stories.tsx

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {Meta, StoryFn, StoryObj} from '@storybook/react';
3333
import NoSearchResults from '@spectrum-icons/illustrations/NoSearchResults';
3434
import {Picker} from '@react-spectrum/picker';
3535
import {Radio, RadioGroup} from '@react-spectrum/radio';
36-
import React, {JSX, useCallback, useEffect, useState} from 'react';
36+
import React, {JSX, useCallback, useState} from 'react';
3737
import {SearchField} from '@react-spectrum/searchfield';
3838
import {Switch} from '@react-spectrum/switch';
3939
import {TextField} from '@react-spectrum/textfield';
@@ -2262,61 +2262,3 @@ export const AsyncLoadOverflowWrapReproStory: TableStory = {
22622262
};
22632263

22642264
export {Performance} from './Performance';
2265-
2266-
2267-
export const Test = {
2268-
render: () => {
2269-
return <SWTable />;
2270-
}
2271-
};
2272-
2273-
function SWTable() {
2274-
const [film, setFilm] = useState(undefined);
2275-
2276-
interface Item {
2277-
id: string,
2278-
title: string
2279-
}
2280-
2281-
let list = useAsyncList<Item>({
2282-
async load({signal, cursor}) {
2283-
console.log('load', cursor, film);
2284-
if (cursor) {
2285-
cursor = cursor.replace(/^http:\/\//i, 'https://');
2286-
}
2287-
let items = [];
2288-
await new Promise(resolve => setTimeout(resolve, 1500));
2289-
let res = await fetch(cursor || `https://swapi.py4e.com/api/people/?search&film=${film}`, {signal});
2290-
let json = await res.json();
2291-
items = json.results.map((element) => ({title: element.name}));
2292-
2293-
return {
2294-
items: items,
2295-
cursor: json.next
2296-
};
2297-
}
2298-
});
2299-
2300-
useEffect(() => {
2301-
list.reload();
2302-
}, [film]);
2303-
2304-
return (
2305-
<div>
2306-
<ActionButton onPress={() => setFilm(undefined)}>All</ActionButton>
2307-
<ActionButton onPress={() => setFilm('https://swapi.py4e.com/api/films/1/')}>A New Hope</ActionButton>
2308-
<TableView aria-label="Characters" height={200} width={400}>
2309-
<TableHeader>
2310-
<Column isRowHeader>Name</Column>
2311-
</TableHeader>
2312-
<TableBody items={list.items} loadingState={list.loadingState} onLoadMore={list.loadMore}>
2313-
{
2314-
item => (<Row key={item.title}>
2315-
<Cell>{item.title}</Cell>
2316-
</Row>)
2317-
}
2318-
</TableBody>
2319-
</TableView>
2320-
</div>
2321-
);
2322-
}

packages/@react-stately/data/test/useAsyncList.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,56 @@ describe('useAsyncList', () => {
334334
expect(result.current.items).toEqual(ITEMS);
335335
});
336336

337+
it('should prevent loadMore from firing if in the middle of a load', async () => {
338+
let load = jest.fn().mockImplementation(getItems);
339+
let {result} = renderHook(
340+
() => useAsyncList({load})
341+
);
342+
343+
expect(load).toHaveBeenCalledTimes(1);
344+
expect(result.current.isLoading).toBe(true);
345+
expect(result.current.items).toEqual([]);
346+
347+
await act(async () => {
348+
result.current.loadMore();
349+
});
350+
351+
expect(result.current.isLoading).toBe(true);
352+
expect(result.current.items).toEqual([]);
353+
expect(load).toHaveBeenCalledTimes(1);
354+
355+
await act(async () => {
356+
jest.runAllTimers();
357+
});
358+
359+
expect(result.current.isLoading).toBe(false);
360+
expect(result.current.items).toEqual(ITEMS);
361+
362+
await act(async () => {
363+
result.current.reload();
364+
});
365+
366+
expect(result.current.isLoading).toBe(true);
367+
expect(result.current.items).toEqual([]);
368+
expect(load).toHaveBeenCalledTimes(2);
369+
370+
await act(async () => {
371+
result.current.loadMore();
372+
});
373+
374+
expect(result.current.isLoading).toBe(true);
375+
expect(result.current.items).toEqual([]);
376+
expect(load).toHaveBeenCalledTimes(2);
377+
378+
await act(async () => {
379+
jest.runAllTimers();
380+
});
381+
382+
expect(result.current.isLoading).toBe(false);
383+
expect(result.current.items).toEqual(ITEMS);
384+
});
385+
386+
337387
it('should ignore duplicate loads where first resolves first', async () => {
338388
let load = jest.fn().mockImplementation(getItems2);
339389
let sort = jest.fn().mockImplementation(() => new Promise(resolve => setTimeout(() => resolve({items: ITEMS2}), 100)));

0 commit comments

Comments
 (0)