From ff8e1b0fc09626eb1347b1dbbdb8195c656d8e2a Mon Sep 17 00:00:00 2001 From: Jim O'Donnell Date: Wed, 5 May 2021 15:30:27 +0100 Subject: [PATCH] Add tests --- .../SubjectPicker/SubjectPicker.spec.js | 94 +++++++++++++++++++ .../helpers/checkRetiredStatus.spec.js | 36 +++++++ .../SubjectPicker/helpers/fetchRows.spec.js | 42 +++++++++ 3 files changed, 172 insertions(+) create mode 100644 packages/app-project/src/shared/components/SubjectPicker/SubjectPicker.spec.js create mode 100644 packages/app-project/src/shared/components/SubjectPicker/helpers/checkRetiredStatus.spec.js create mode 100644 packages/app-project/src/shared/components/SubjectPicker/helpers/fetchRows.spec.js diff --git a/packages/app-project/src/shared/components/SubjectPicker/SubjectPicker.spec.js b/packages/app-project/src/shared/components/SubjectPicker/SubjectPicker.spec.js new file mode 100644 index 0000000000..15e32294a6 --- /dev/null +++ b/packages/app-project/src/shared/components/SubjectPicker/SubjectPicker.spec.js @@ -0,0 +1,94 @@ +import { mount, shallow } from 'enzyme' +import { Grommet } from 'grommet' +import nock from 'nock' +import zooTheme from '@zooniverse/grommet-theme' + +import SubjectPicker, { StyledBox, SubjectDataTable } from './SubjectPicker' + +describe('Components > Subject Picker', function () { + let wrapper + const workflow = { + id: '123345' + } + const subjectSet = { + id: '4567', + display_name: 'Test subject set', + metadata: { + indexFields: 'Date,Page' + } + } + + before(function () { + wrapper = shallow( + + ) + }) + + it('should render', function () { + expect(wrapper).to.be.ok() + }) + + it('should show the subject set name', function () { + const displayName = wrapper.find(StyledBox) + expect(displayName).to.be.ok() + }) + + describe('subject data table', function () { + let wrapper + + before(async function () { + const columns = [ + 'subject_id', + 'Page', + 'Date' + ] + const rows = [ + ['1', '43', '23 January 1916'], + ['2', '44', '24 January 1916'], + ['3', '45', '25 January 1916'] + ] + nock('https://subject-set-search-api.zooniverse.org/subjects') + .get('/4567.json') + .query(true) + .reply(200, { + columns, + rows + }) + nock('https://panoptes-staging.zooniverse.org/api') + .get('/subject_workflow_statuses') + .query(true) + .reply(200, { + subject_workflow_statuses: [ + { classifications_count: 0, retired_at: null, links: { subject: '1' }}, + { classifications_count: 3, retired_at: null, links: { subject: '2' }}, + { classifications_count: 5, retired_at: "2018-01-30T21:09:49.396Z", links: { subject: '3' }} + ] + }) + wrapper = mount( + , + { wrappingComponent: Grommet, wrappingComponentProps: { theme: zooTheme } } + ) + // workaround to wait for the mock API calls to resolve + await new Promise((resolve) => setTimeout(resolve, 100)); + }) + + it('should have column headings, including indexed subject fields', function () { + const dataTable = wrapper.find(SubjectDataTable) + const headers = dataTable.prop('columns').map(column => column.header) + expect(headers).to.deep.equal(['subject_id', 'Date', 'Page', 'status']) + }) + + it('should have a row for each subject', function () { + wrapper.update() + const dataTable = wrapper.find(SubjectDataTable) + expect(dataTable.prop('data').length).to.equal(3) + }) + }) +}) \ No newline at end of file diff --git a/packages/app-project/src/shared/components/SubjectPicker/helpers/checkRetiredStatus.spec.js b/packages/app-project/src/shared/components/SubjectPicker/helpers/checkRetiredStatus.spec.js new file mode 100644 index 0000000000..1808f46ae5 --- /dev/null +++ b/packages/app-project/src/shared/components/SubjectPicker/helpers/checkRetiredStatus.spec.js @@ -0,0 +1,36 @@ +import nock from 'nock' +import checkRetiredStatus from './checkRetiredStatus' + +describe('Components > Subject Picker > helpers > checkRetiredStatus', function () { + let retirementStatuses + + before(async function () { + const subject_ids = ['1', '2', '3'] + const workflow = { + id: '1' + } + const panoptes = nock('https://panoptes-staging.zooniverse.org/api') + .get('/subject_workflow_statuses') + .query(true) + .reply(200, { + subject_workflow_statuses: [ + { classifications_count: 0, retired_at: null, links: { subject: '1' }}, + { classifications_count: 3, retired_at: null, links: { subject: '2' }}, + { classifications_count: 5, retired_at: "2018-01-30T21:09:49.396Z", links: { subject: '3' }} + ] + }) + retirementStatuses = await checkRetiredStatus(subject_ids, workflow) + }) + + it('should set the status of unclassified subjects', function () { + expect(retirementStatuses['1']).to.equal('Unclassified') + }) + + it('should set the status of classified subjects', function () { + expect(retirementStatuses['2']).to.equal('In progress') + }) + + it('should set the status of retired subjects', function () { + expect(retirementStatuses['3']).to.equal('Retired') + }) +}) \ No newline at end of file diff --git a/packages/app-project/src/shared/components/SubjectPicker/helpers/fetchRows.spec.js b/packages/app-project/src/shared/components/SubjectPicker/helpers/fetchRows.spec.js new file mode 100644 index 0000000000..25690ec561 --- /dev/null +++ b/packages/app-project/src/shared/components/SubjectPicker/helpers/fetchRows.spec.js @@ -0,0 +1,42 @@ +import nock from 'nock' +import fetchRows from './fetchRows' + +describe('Components > Subject Picker > helpers > fetchRows', function () { + let data + const expectedData = [ + { subject_id: '1', Page: '43', Date: '23 January 1916', status: 'Unclassified' }, + { subject_id: '2', Page: '44', Date: '24 January 1916', status: 'In progress' }, + { subject_id: '3', Page: '45', Date: '25 January 1916', status: 'Retired' }, + ] + + before(async function () { + const columns = [ + 'subject_id', + 'Page', + 'Date' + ] + const rows = [ + ['1', '43', '23 January 1916'], + ['2', '44', '24 January 1916'], + ['3', '45', '25 January 1916'] + ] + const workflow = { + id: '1' + } + const panoptes = nock('https://panoptes-staging.zooniverse.org/api') + .get('/subject_workflow_statuses') + .query(true) + .reply(200, { + subject_workflow_statuses: [ + { classifications_count: 0, retired_at: null, links: { subject: '1' }}, + { classifications_count: 3, retired_at: null, links: { subject: '2' }}, + { classifications_count: 5, retired_at: "2018-01-30T21:09:49.396Z", links: { subject: '3' }} + ] + }) + data = await fetchRows({ columns, rows }, workflow) + }) + + it('should generate subject data table rows with classification statuses', function () { + expect(data).to.deep.equal(expectedData) + }) +}) \ No newline at end of file