diff --git a/web/src/App.test.tsx b/web/src/App.test.tsx index 2a68616..ed9723f 100644 --- a/web/src/App.test.tsx +++ b/web/src/App.test.tsx @@ -1,9 +1,38 @@ import React from 'react'; import { render, screen } from '@testing-library/react'; +import { MockedProvider } from '@apollo/client/testing'; import App from './App'; +import { AllPostsDocument } from './rpc/operations/AllPosts.generated'; -test('renders learn react link', () => { - render(); - const linkElement = screen.getByText(/learn react/i); - expect(linkElement).toBeInTheDocument(); +test('renders app title', () => { + const mocks = [ + { + request: { + query: AllPostsDocument, + variables: { cursor: null, limit: 10 }, + }, + result: { + data: { + listPosts: { + edges: [], + pageInfo: { + endCursor: null, + startCursor: null, + hasPreviousPage: false, + hasNextPage: false, + }, + }, + }, + }, + }, + ]; + + render( + + + , + ); + + expect(screen.getByText(/sniff/i)).toBeInTheDocument(); }); + diff --git a/web/src/components/post.tsx b/web/src/components/post.tsx index 17b9185..ea3a5c0 100644 --- a/web/src/components/post.tsx +++ b/web/src/components/post.tsx @@ -15,6 +15,7 @@ const PostDateTime = styled.span` `; type PostProps = { + id: string; title: string; body: string; created_at: string; diff --git a/web/src/components/postList.test.tsx b/web/src/components/postList.test.tsx new file mode 100644 index 0000000..d2c6edb --- /dev/null +++ b/web/src/components/postList.test.tsx @@ -0,0 +1,25 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import PostList from './postList'; +import { Post as PostData } from '../rpc/types'; + +test('maintains element identity when post order changes', () => { + const posts: PostData[] = [ + { id: '1', title: 'First', body: 'Body1', created_at: '2023-01-01' }, + { id: '2', title: 'Second', body: 'Body2', created_at: '2023-01-02' }, + ]; + + const { rerender } = render(); + + const firstNodeInitial = screen.getByText('First').closest('li'); + const secondNodeInitial = screen.getByText('Second').closest('li'); + + rerender(); + + const firstNodeAfter = screen.getByText('First').closest('li'); + const secondNodeAfter = screen.getByText('Second').closest('li'); + + expect(firstNodeAfter).toBe(firstNodeInitial); + expect(secondNodeAfter).toBe(secondNodeInitial); +}); + diff --git a/web/src/components/postList.tsx b/web/src/components/postList.tsx index 8551b08..3e2bb1a 100644 --- a/web/src/components/postList.tsx +++ b/web/src/components/postList.tsx @@ -13,8 +13,8 @@ function PostList({ posts, loading }: PostListParams) { { loading || posts === undefined ? Loading... :
    - {posts.map((post, index) => -
  1. + {posts.map((post) => +
  2. )}