Skip to content

Commit

Permalink
Fix bug with setting active item index compared to items list length
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarah Allen committed Apr 8, 2019
1 parent b3f9b7a commit 211c0a7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/lib-classifier/src/store/FieldGuideStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const FieldGuideStore = types

function setActiveItemIndex (index) {
const fieldGuide = self.active
if (fieldGuide && fieldGuide.items.length === index + 1 && fieldGuide.items[index]) {
if (fieldGuide && index + 1 <= fieldGuide.items.length && fieldGuide.items[index]) {
if (fieldGuide.items[index].icon) self.activeMedium = fieldGuide.items[index].icon
return self.activeItemIndex = index
}
Expand Down
35 changes: 31 additions & 4 deletions packages/lib-classifier/src/store/FieldGuideStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const fieldGuideWithItems = FieldGuideFactory.build({ items: [
content: 'All about cats',
icon: medium.id,
title: 'Cats'
},
{
content: 'All about dogs',
title: 'Dogs'
}
]})

Expand All @@ -32,7 +36,7 @@ const fieldGuideWithoutIcon = FieldGuideFactory.build({

const project = ProjectFactory.build()

describe('Model > FieldGuideStore', function () {
describe.only('Model > FieldGuideStore', function () {
let rootStore
function fetchFieldGuide () {
sinon.stub(rootStore.fieldGuide, 'fetchFieldGuide')
Expand Down Expand Up @@ -341,7 +345,7 @@ describe('Model > FieldGuideStore', function () {
})

fetchFieldGuide().then(() => {
rootStore.fieldGuide.setActiveItemIndex(1)
rootStore.fieldGuide.setActiveItemIndex(2)
expect(rootStore.fieldGuide.activeItemIndex).to.be.undefined
expect(rootStore.fieldGuide.activeMedium).to.be.undefined
}).then(done, done)
Expand All @@ -362,12 +366,35 @@ describe('Model > FieldGuideStore', function () {
}
})

fetchFieldGuide()
.then(() => {
fieldGuideWithItems.items.forEach((item, index) => {
rootStore.fieldGuide.setActiveItemIndex(index)
expect(rootStore.fieldGuide.activeItemIndex).to.equal(index)
})
}).then(done, done)
})

it('should set the active medium if the item has an icon', function (done) {
rootStore = RootStore.create({
fieldGuide: FieldGuideStore.create(),
projects: ProjectStore.create()
}, {
client: {
panoptes: {
get: sinon.stub().callsFake((url) => {
if (url === '/field_guides') return Promise.resolve({ body: { field_guides: [fieldGuideWithItems] } })
if (url === `/field_guides/${fieldGuideWithItems.id}/attached_images`) return Promise.resolve({ body: { media: [medium] } })
})
}
}
})

fetchFieldGuide()
.then(() => {
rootStore.fieldGuide.setActiveItemIndex(0)
expect(rootStore.fieldGuide.activeItemIndex).to.equal(0)
expect(rootStore.fieldGuide.activeMedium.toJSON()).to.deep.equal(medium)
}).then(done, done)
}).then(done, done)
})

it('should not set the active medium if the item does not have an icon', function (done) {
Expand Down

0 comments on commit 211c0a7

Please sign in to comment.