Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions web/src/App.test.tsx
Original file line number Diff line number Diff line change
@@ -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(<App />);
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(
<MockedProvider mocks={mocks} addTypename={false}>
<App />
</MockedProvider>,
);

expect(screen.getByText(/sniff/i)).toBeInTheDocument();
});

1 change: 1 addition & 0 deletions web/src/components/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const PostDateTime = styled.span`
`;

type PostProps = {
id: string;
title: string;
body: string;
created_at: string;
Expand Down
25 changes: 25 additions & 0 deletions web/src/components/postList.test.tsx
Original file line number Diff line number Diff line change
@@ -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(<PostList posts={posts} loading={false} />);

const firstNodeInitial = screen.getByText('First').closest('li');
const secondNodeInitial = screen.getByText('Second').closest('li');

rerender(<PostList posts={[posts[1], posts[0]]} loading={false} />);

const firstNodeAfter = screen.getByText('First').closest('li');
const secondNodeAfter = screen.getByText('Second').closest('li');

expect(firstNodeAfter).toBe(firstNodeInitial);
expect(secondNodeAfter).toBe(secondNodeInitial);
});

4 changes: 2 additions & 2 deletions web/src/components/postList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function PostList({ posts, loading }: PostListParams) {
{ loading || posts === undefined ?
<em>Loading...</em> :
<ol>
{posts.map((post, index) =>
<li key={`post_${index}`}>
{posts.map((post) =>
<li key={post.id}>
<Post {...post}/>
</li>
)}
Expand Down