|
| 1 | +import { describe, it, beforeEach } from 'mocha' |
| 2 | +import { expect } from 'chai' |
| 3 | +import { jsonReader } from '../utility/fileOperations/readwrite' |
| 4 | +import { contentstackClient } from '../utility/ContentstackClient.js' |
| 5 | + |
| 6 | +var client = {} |
| 7 | + |
| 8 | +const taxonomy = { |
| 9 | + uid: 'taxonomy_testing', |
| 10 | + name: 'taxonomy testing', |
| 11 | + description: 'Description for Taxonomy testing' |
| 12 | +} |
| 13 | +const termString = 'term' |
| 14 | +const term = { |
| 15 | + term: { |
| 16 | + uid: 'term_test', |
| 17 | + name: 'Term test', |
| 18 | + parent_uid: null |
| 19 | + } |
| 20 | +} |
| 21 | +const childTerm1 = { |
| 22 | + term: { |
| 23 | + uid: 'term_test_child1', |
| 24 | + name: 'Term test1', |
| 25 | + parent_uid: 'term_test' |
| 26 | + } |
| 27 | +} |
| 28 | +const childTerm2 = { |
| 29 | + term: { |
| 30 | + uid: 'term_test_child2', |
| 31 | + name: 'Term test2', |
| 32 | + parent_uid: 'term_test_child1' |
| 33 | + } |
| 34 | +} |
| 35 | +var termUid = term.term.uid |
| 36 | + |
| 37 | +describe('Terms API Test', () => { |
| 38 | + beforeEach(() => { |
| 39 | + const user = jsonReader('loggedinuser.json') |
| 40 | + client = contentstackClient(user.authtoken) |
| 41 | + }) |
| 42 | + it('should create taxonomy', async () => { |
| 43 | + await client.stack({ api_key: process.env.API_KEY }).taxonomy().create({ taxonomy }) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should create term', done => { |
| 47 | + makeTerms(taxonomy.uid).create(term) |
| 48 | + .then((response) => { |
| 49 | + expect(response.uid).to.be.equal(term.term.uid) |
| 50 | + done() |
| 51 | + }) |
| 52 | + .catch(done) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should create child term 1', done => { |
| 56 | + makeTerms(taxonomy.uid).create(childTerm1) |
| 57 | + .then((response) => { |
| 58 | + expect(response.uid).to.be.equal(childTerm1.term.uid) |
| 59 | + done() |
| 60 | + }) |
| 61 | + .catch(done) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should create child term 2', done => { |
| 65 | + makeTerms(taxonomy.uid).create(childTerm2) |
| 66 | + .then((response) => { |
| 67 | + expect(response.uid).to.be.equal(childTerm2.term.uid) |
| 68 | + done() |
| 69 | + }) |
| 70 | + .catch(done) |
| 71 | + }) |
| 72 | + |
| 73 | + it('should query and get all terms', done => { |
| 74 | + makeTerms(taxonomy.uid).query().find() |
| 75 | + .then((response) => { |
| 76 | + expect(response.items).to.be.an('array') |
| 77 | + expect(response.items[0].uid).not.to.be.equal(null) |
| 78 | + expect(response.items[0].name).not.to.be.equal(null) |
| 79 | + done() |
| 80 | + }) |
| 81 | + .catch(done) |
| 82 | + }) |
| 83 | + |
| 84 | + it('should fetch term of the term uid passed', done => { |
| 85 | + makeTerms(taxonomy.uid, term.term.uid).fetch() |
| 86 | + .then((response) => { |
| 87 | + expect(response.uid).to.be.equal(termUid) |
| 88 | + expect(response.name).not.to.be.equal(null) |
| 89 | + expect(response.created_by).not.to.be.equal(null) |
| 90 | + expect(response.updated_by).not.to.be.equal(null) |
| 91 | + done() |
| 92 | + }) |
| 93 | + .catch(done) |
| 94 | + }) |
| 95 | + |
| 96 | + it('should update term of the term uid passed', done => { |
| 97 | + makeTerms(taxonomy.uid, termUid).fetch() |
| 98 | + .then((term) => { |
| 99 | + term.name = 'update name' |
| 100 | + return term.update() |
| 101 | + }) |
| 102 | + .then((response) => { |
| 103 | + expect(response.uid).to.be.equal(termUid) |
| 104 | + expect(response.name).to.be.equal('update name') |
| 105 | + expect(response.created_by).not.to.be.equal(null) |
| 106 | + expect(response.updated_by).not.to.be.equal(null) |
| 107 | + done() |
| 108 | + }) |
| 109 | + .catch(done) |
| 110 | + }) |
| 111 | + |
| 112 | + it('should get the ancestors of the term uid passed', done => { |
| 113 | + makeTerms(taxonomy.uid, childTerm1.term.uid).ancestors() |
| 114 | + .then((response) => { |
| 115 | + expect(response.terms[0].uid).not.to.be.equal(null) |
| 116 | + expect(response.terms[0].name).not.to.be.equal(null) |
| 117 | + expect(response.terms[0].created_by).not.to.be.equal(null) |
| 118 | + expect(response.terms[0].updated_by).not.to.be.equal(null) |
| 119 | + done() |
| 120 | + }) |
| 121 | + .catch(done) |
| 122 | + }) |
| 123 | + |
| 124 | + it('should get the descendants of the term uid passed', done => { |
| 125 | + makeTerms(taxonomy.uid, childTerm1.term.uid).descendants() |
| 126 | + .then((response) => { |
| 127 | + expect(response.terms.uid).not.to.be.equal(null) |
| 128 | + expect(response.terms.name).not.to.be.equal(null) |
| 129 | + expect(response.terms.created_by).not.to.be.equal(null) |
| 130 | + expect(response.terms.updated_by).not.to.be.equal(null) |
| 131 | + done() |
| 132 | + }) |
| 133 | + .catch(done) |
| 134 | + }) |
| 135 | + |
| 136 | + it('should search the term with the string passed', done => { |
| 137 | + makeTerms(taxonomy.uid).search(termString) |
| 138 | + .then((response) => { |
| 139 | + expect(response.terms).to.be.an('array') |
| 140 | + done() |
| 141 | + }) |
| 142 | + .catch(done) |
| 143 | + }) |
| 144 | + |
| 145 | + it('should move the term to parent uid passed', done => { |
| 146 | + makeTerms(taxonomy.uid, childTerm2.term.uid).move({ force: true }) |
| 147 | + .then(async (term) => { |
| 148 | + term.parent_uid = null |
| 149 | + const moveTerm = await term.move({ force: true }) |
| 150 | + expect(moveTerm.parent_uid).to.be.equal(null) |
| 151 | + done() |
| 152 | + return moveTerm |
| 153 | + }) |
| 154 | + .catch(done) |
| 155 | + }) |
| 156 | + |
| 157 | + it('should delete of the term uid passed', done => { |
| 158 | + makeTerms(taxonomy.uid, term.term.uid).delete({ force: true }) |
| 159 | + .then((response) => { |
| 160 | + expect(response.status).to.be.equal(204) |
| 161 | + done() |
| 162 | + }) |
| 163 | + .catch(done) |
| 164 | + }) |
| 165 | + |
| 166 | + it('should delete taxonomy', async () => { |
| 167 | + const taxonomyResponse = await client.stack({ api_key: process.env.API_KEY }).taxonomy(taxonomy.uid).delete({ force: true }) |
| 168 | + expect(taxonomyResponse.status).to.be.equal(204) |
| 169 | + }) |
| 170 | +}) |
| 171 | + |
| 172 | +function makeTerms (taxonomyUid, termUid = null) { |
| 173 | + return client.stack({ api_key: process.env.API_KEY }).taxonomy(taxonomyUid).terms(termUid) |
| 174 | +} |
0 commit comments