Skip to content

Commit

Permalink
test: ✅ unit test cases for terms implementation (except search and m…
Browse files Browse the repository at this point in the history
…ove)
  • Loading branch information
harshithad0703 committed Sep 28, 2023
1 parent a0e855e commit 96ba089
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 11 deletions.
13 changes: 2 additions & 11 deletions lib/stack/taxonomy/terms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
fetch,
update,
query,
deleteEntity
deleteEntity,
parseData
} from '../../../entity'

export function Terms (http, data) {
Expand Down Expand Up @@ -126,16 +127,6 @@ export function Terms (http, data) {
this.query = query({ http: http, wrapperCollection: TermsCollection })
}
}
export function parseData (response, stackHeaders, taxonomy_uid) {
const data = response.data || {}
if (stackHeaders) {
data.stackHeaders = stackHeaders
}
if (taxonomy_uid) {
data.taxonomy_uid = taxonomy_uid
}
return data
}
export function TermsCollection (http, data) {
const obj = cloneDeep(data.terms) || []
const termsCollection = obj.map((term) => {
Expand Down
1 change: 1 addition & 0 deletions test/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ require('./app-request-test')
require('./authorization-test')
require('./auditLog-test')
require('./taxonomy-test')
require('./terms-test')
42 changes: 42 additions & 0 deletions test/unit/mock/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,47 @@ const taxonomyMock = {
referenced_terms_count: 3,
referenced_entries_count: 6
}
const termsMock = {
taxonomy_uid: 'taxonomy_uid',
uid: 'UID',
name: 'name',
parent_uid: 'term_2',
depth: 2,
children_count: 2,
referenced_entries_count: 2,
ancestors: [{
uid: 'term_1',
name: 'Term 1',
parent_uid: null,
depth: 1,
children_count: 3,
referenced_entries_count: 3
},
{
uid: 'term_2',
name: 'Term 2',
parent_uid: 'term_1',
depth: 2,
children_count: 2,
referenced_entries_count: 2
}],
descendants: [{
uid: 'term_4',
name: 'Term 4',
parent_uid: 'term_3',
depth: 3,
children_count: 1,
referenced_entries_count: 2
},
{
uid: 'term_5',
name: 'Term 5',
parent_uid: 'term_4',
depth: 4,
children_count: 0,
referenced_entries_count: 4
}]
}

function mockCollection (mockData, type) {
const mock = {
Expand Down Expand Up @@ -803,6 +844,7 @@ export {
auditLogsMock,
auditLogItemMock,
taxonomyMock,
termsMock,
mockCollection,
entryMockCollection,
checkSystemFields
Expand Down
172 changes: 172 additions & 0 deletions test/unit/terms-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import Axios from 'axios'
import { expect } from 'chai'
import { describe, it } from 'mocha'
import MockAdapter from 'axios-mock-adapter'
import { Terms } from '../../lib/stack/taxonomy/terms'
import { systemUidMock, stackHeadersMock, termsMock, noticeMock } from './mock/objects'

describe('Contentstack Term test', () => {
it('term create test', done => {
var mock = new MockAdapter(Axios)
mock.onPost(`/taxonomies/taxonomy_uid/terms`).reply(200, {
term: {
...termsMock
}
})
makeTerms()
.create()
.then((term) => {
checkTerms(term)
done()
})
.catch(done)
})
it('Term fetch test', done => {
var mock = new MockAdapter(Axios)
mock.onGet(`/taxonomies/taxonomy_uid/terms/UID`).reply(200, {
term: {
...termsMock
}
})
makeTerms({
term: {
...systemUidMock
},
stackHeaders: stackHeadersMock
})
.fetch()
.then((term) => {
checkTerms(term)
done()
})
.catch(done)
})
it('Terms query test', done => {
var mock = new MockAdapter(Axios)
mock.onGet(`/taxonomies/taxonomy_uid/terms`).reply(200, {
terms: [
termsMock
]
})
makeTerms()
.query()
.find()
.then((terms) => {
checkTerms(terms.items[0])
done()
})
.catch(done)
})
it('Term update test', done => {
var mock = new MockAdapter(Axios)
mock.onPut(`/taxonomies/taxonomy_uid/terms/UID`).reply(200, {
term: {
...termsMock
}
})
makeTerms({
term: {
...systemUidMock
},
stackHeaders: stackHeadersMock
})
.update()
.then((term) => {
checkTerms(term)
done()
})
.catch(done)
})
it('term delete test', done => {
var mock = new MockAdapter(Axios)
mock.onDelete(`/taxonomies/taxonomy_uid/terms/UID`).reply(200, {
...noticeMock
})
makeTerms({
term: {
...systemUidMock
},
stackHeaders: stackHeadersMock
})
.delete()
.then((term) => {
expect(term.notice).to.be.equal(noticeMock.notice)
done()
})
.catch(done)
})
it('Term ancestors test', done => {
var mock = new MockAdapter(Axios)
mock.onGet(`/taxonomies/taxonomy_uid/terms/UID/ancestors`).reply(200, { ...termsMock })
makeTerms({
term: {
...systemUidMock
},
stackHeaders: stackHeadersMock
})
.ancestors()
.then((terms) => {
checkTerms(terms)
expect(terms.uid).to.be.equal('UID')
expect(terms.parent_uid).to.be.equal('term_2')
expect(terms.ancestors[0].uid).to.be.equal('term_1')
expect(terms.ancestors[1].uid).to.be.equal('term_2')
expect(terms.ancestors[1].parent_uid).to.be.equal('term_1')
done()
})
.catch(done)
})
it('Term descendants test', done => {
var mock = new MockAdapter(Axios)
mock.onGet(`/taxonomies/taxonomy_uid/terms/UID/descendants`).reply(200, { ...termsMock })
makeTerms({
term: {
...systemUidMock
},
stackHeaders: stackHeadersMock
})
.descendants()
.then((terms) => {
checkTerms(terms)
expect(terms.uid).to.be.equal('UID')
expect(terms.descendants[0].uid).to.be.equal('term_4')
expect(terms.descendants[1].uid).to.be.equal('term_5')
expect(terms.descendants[1].parent_uid).to.be.equal('term_4')
done()
})
.catch(done)
})
it('Term test without term uid', done => {
const term = makeTerms()
expect(term.stackHeaders).to.be.equal(undefined)
expect(term.update).to.be.equal(undefined)
expect(term.delete).to.be.equal(undefined)
expect(term.fetch).to.be.equal(undefined)
expect(term.create).not.to.be.equal(undefined)
expect(term.query).not.to.be.equal(undefined)
done()
})
it('Term test with term uid', done => {
const term = makeTerms({
term: {
...systemUidMock
}
})
expect(term.urlPath).to.be.equal(`/taxonomies/taxonomy_uid/terms/${systemUidMock.uid}`)
expect(term.stackHeaders).to.be.equal(undefined)
expect(term.update).not.to.be.equal(undefined)
expect(term.delete).not.to.be.equal(undefined)
expect(term.fetch).not.to.be.equal(undefined)
expect(term.create).to.be.equal(undefined)
expect(term.query).to.be.equal(undefined)
done()
})
})

function makeTerms (data = {}) {
return new Terms(Axios, { taxonomy_uid: 'taxonomy_uid', ...data })
}

function checkTerms (terms) {
expect(terms.name).to.be.equal('name')
}

0 comments on commit 96ba089

Please sign in to comment.