diff --git a/__mocks__/queries.js b/__mocks__/queries.js index 42ccb90..88a8041 100644 --- a/__mocks__/queries.js +++ b/__mocks__/queries.js @@ -44,6 +44,50 @@ const repositories = { } }; +const sortedRepositories = { + request: { + query: LIST_REPOSITORIES, + variables: { + first: 20, + after: null, + orderBy: 'STARGAZERS', + orderDirection: 'DESC' + } + }, + result: { + data: { + user: { + repositories: { + pageInfo: { + hasNextPage: true, + endCursor: + 'Y3Vyc29yOnYyOpK5MjAxOS0wMy0yNFQxNTozNjo1OS0wMzowMM4Kk7uL' + }, + nodes: [ + { + id: 'MDEwOlJlcG9zaXRvcnkyMDc0ODYxMDY=', + name: 'my-repositories', + description: null, + isPrivate: false, + forkCount: 0, + updatedAt: '2019-09-11T06:53:35Z', + url: 'https://github.com/CristianeMayara/my-repositories', + primaryLanguage: { + name: 'JavaScript', + color: '#f1e05a' + }, + licenseInfo: null, + stargazers: { + totalCount: 0 + } + } + ] + } + } + } + } +}; + const commits = { request: { query: LIST_COMMITS, @@ -103,4 +147,4 @@ const commits = { } }; -export { repositories, commits }; +export { repositories, sortedRepositories, commits }; diff --git a/src/screens/RepositoryList/RepositoryList.spec.js b/src/screens/RepositoryList/RepositoryList.spec.js index a895200..7d36992 100644 --- a/src/screens/RepositoryList/RepositoryList.spec.js +++ b/src/screens/RepositoryList/RepositoryList.spec.js @@ -1,9 +1,18 @@ import React from 'react'; import { MockedProvider } from '@apollo/react-testing'; -import { render, cleanup } from '@testing-library/react'; +import { + render, + cleanup, + fireEvent, + waitForElement +} from '@testing-library/react'; import wait from 'waait'; import App from '../../App'; -import { repositories, commits } from '../../../__mocks__/queries'; +import { + sortedRepositories, + repositories, + commits +} from '../../../__mocks__/queries'; describe('RepositoryList component', () => { afterEach(cleanup); @@ -20,13 +29,33 @@ describe('RepositoryList component', () => { expect(getByText('my-repositories')).toBeInTheDocument(); }); }); + + describe('when click on sort by startgazers button', () => { + it('should sort the list of repositories', async () => { + const { queryByTestId, getByTestId, getByText } = createComponent(); + + await waitForElement(() => queryByTestId('list-title')); + + fireEvent.click(getByText(/Mais estrelas/i)); + + await wait(() => + expect(queryByTestId('loading-icon')).not.toBeInTheDocument() + ); + + expect(getByTestId('list-title')).toBeInTheDocument(); + expect(getByText('my-repositories')).toBeInTheDocument(); + }); + }); }); function createComponent(props = {}) { const defaultProps = { ...props }; return render( - + );