Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eatyourgreens committed May 12, 2021
1 parent 4f4e989 commit ff8e1b0
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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(
<SubjectPicker
baseUrl="/workflow/12345/subject-set/4567"
subjectSet={subjectSet}
workflow={workflow}
/>
)
})

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(
<SubjectPicker
subjectSet={subjectSet}
workflow={workflow}
/>,
{ 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)
})
})
})
Original file line number Diff line number Diff line change
@@ -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')
})
})
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit ff8e1b0

Please sign in to comment.