|
| 1 | +import { expect } from "chai"; |
| 2 | +import { describe, it, setup } from "mocha"; |
| 3 | +import { jsonReader } from "../utility/fileOperations/readwrite"; |
| 4 | +import { createVariantGroup } from "../mock/variantGroup.js"; |
| 5 | +import { variant } from "../mock/variants.js"; |
| 6 | +import { |
| 7 | + variantEntryFirst, |
| 8 | + publishVariantEntryFirst, |
| 9 | + unpublishVariantEntryFirst, |
| 10 | +} from "../mock/variantEntry.js"; |
| 11 | +import { contentstackClient } from "../utility/ContentstackClient.js"; |
| 12 | + |
| 13 | +var client = {}; |
| 14 | + |
| 15 | +var stack = {}; |
| 16 | +var variantUid = ""; |
| 17 | +var variantGroupUid = ""; |
| 18 | +var contentTypeUid = ""; |
| 19 | +var entryUid = ""; |
| 20 | + |
| 21 | +describe("Entry Variants api Test", () => { |
| 22 | + setup(() => { |
| 23 | + const user = jsonReader("loggedinuser.json"); |
| 24 | + stack = jsonReader("stack.json"); |
| 25 | + client = contentstackClient(user.authtoken); |
| 26 | + const entry = jsonReader("entry.json"); |
| 27 | + entryUid = entry[2].uid; |
| 28 | + contentTypeUid = entry[2].content_type_uid; |
| 29 | + }); |
| 30 | + |
| 31 | + it("should create a Variant Group", (done) => { |
| 32 | + makeVariantGroup() |
| 33 | + .create(createVariantGroup) |
| 34 | + .then((variantGroup) => { |
| 35 | + variantGroupUid = variantGroup.uid; |
| 36 | + expect(variantGroup.name).to.be.equal(createVariantGroup.name); |
| 37 | + expect(variantGroup.uid).to.be.equal(createVariantGroup.uid); |
| 38 | + done(); |
| 39 | + }) |
| 40 | + .catch(done); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should create a Variants", (done) => { |
| 44 | + makeVariants() |
| 45 | + .create(variant) |
| 46 | + .then((variants) => { |
| 47 | + variantUid = variants.uid; |
| 48 | + expect(variants.name).to.be.equal(variant.name); |
| 49 | + expect(variants.uid).to.be.not.equal(null); |
| 50 | + done(); |
| 51 | + }) |
| 52 | + .catch(done); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should update/create variant of an entry", (done) => { |
| 56 | + makeEntryVariants(variantUid) |
| 57 | + .update(variantEntryFirst) |
| 58 | + .then((variantEntry) => { |
| 59 | + expect(variantEntry.entry.title).to.be.equal("First page variant"); |
| 60 | + expect(variantEntry.entry._variant._uid).to.be.not.equal(null); |
| 61 | + expect(variantEntry.notice).to.be.equal( |
| 62 | + "Entry variant created successfully." |
| 63 | + ); |
| 64 | + done(); |
| 65 | + }) |
| 66 | + .catch(done); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should get an entry variant", (done) => { |
| 70 | + makeEntryVariants(variantUid) |
| 71 | + .fetch(variantUid) |
| 72 | + .then((variantEntry) => { |
| 73 | + expect(variantEntry.entry.title).to.be.equal("First page variant"); |
| 74 | + expect(variantEntry.entry._variant._uid).to.be.not.equal(null); |
| 75 | + done(); |
| 76 | + }) |
| 77 | + .catch(done); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should publish entry variant", (done) => { |
| 81 | + publishVariantEntryFirst.entry.variants[0].uid = variantUid; |
| 82 | + |
| 83 | + makeEntry() |
| 84 | + .entry(entryUid) |
| 85 | + .publish({ |
| 86 | + publishDetails: publishVariantEntryFirst.entry, |
| 87 | + locale: publishVariantEntryFirst.locale, |
| 88 | + }) |
| 89 | + .then((data) => { |
| 90 | + expect(data.notice).to.be.equal( |
| 91 | + "The requested action has been performed." |
| 92 | + ); |
| 93 | + expect(data.job_id).to.be.not.equal(null); |
| 94 | + done(); |
| 95 | + }) |
| 96 | + .catch(done); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should unpublish entry variant", (done) => { |
| 100 | + unpublishVariantEntryFirst.entry.variants[0].uid = variantUid; |
| 101 | + makeEntry() |
| 102 | + .entry(entryUid) |
| 103 | + .unpublish({ |
| 104 | + publishDetails: publishVariantEntryFirst.entry, |
| 105 | + locale: publishVariantEntryFirst.locale, |
| 106 | + }) |
| 107 | + .then((data) => { |
| 108 | + expect(data.notice).to.be.equal( |
| 109 | + "The requested action has been performed." |
| 110 | + ); |
| 111 | + expect(data.job_id).to.be.not.equal(null); |
| 112 | + done(); |
| 113 | + }) |
| 114 | + .catch(done); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should publish entry variant using api_version", (done) => { |
| 118 | + publishVariantEntryFirst.entry.variants[0].uid = variantUid; |
| 119 | + makeEntry() |
| 120 | + .entry(entryUid, { api_version: "3.2" }) |
| 121 | + .publish({ |
| 122 | + publishDetails: publishVariantEntryFirst.entry, |
| 123 | + locale: publishVariantEntryFirst.locale, |
| 124 | + }) |
| 125 | + .then((data) => { |
| 126 | + expect(data.notice).to.be.equal( |
| 127 | + "The requested action has been performed." |
| 128 | + ); |
| 129 | + expect(data.job_id).to.be.not.equal(null); |
| 130 | + done(); |
| 131 | + }) |
| 132 | + .catch(done); |
| 133 | + }); |
| 134 | + |
| 135 | + it("should unpublish entry variant using api_version", (done) => { |
| 136 | + unpublishVariantEntryFirst.entry.variants[0].uid = variantUid; |
| 137 | + makeEntry() |
| 138 | + .entry(entryUid, { api_version: "3.2" }) |
| 139 | + .unpublish({ |
| 140 | + publishDetails: unpublishVariantEntryFirst.entry, |
| 141 | + locale: unpublishVariantEntryFirst.locale, |
| 142 | + }) |
| 143 | + .then((data) => { |
| 144 | + expect(data.notice).to.be.equal( |
| 145 | + "The requested action has been performed." |
| 146 | + ); |
| 147 | + expect(data.job_id).to.be.not.equal(null); |
| 148 | + done(); |
| 149 | + }) |
| 150 | + .catch(done); |
| 151 | + }); |
| 152 | + it("should get all entry variants", (done) => { |
| 153 | + makeEntryVariants() |
| 154 | + .query({}) |
| 155 | + .find() |
| 156 | + .then((variantEntries) => { |
| 157 | + expect(variantEntries.items).to.be.an("array"); |
| 158 | + expect(variantEntries.items[0].variants.title).to.be.equal( |
| 159 | + "First page variant" |
| 160 | + ); |
| 161 | + expect(variantEntries.items[0].variants._variant._uid).to.be.not.equal( |
| 162 | + null |
| 163 | + ); |
| 164 | + done(); |
| 165 | + }) |
| 166 | + .catch(done); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should delete entry variant from uid", (done) => { |
| 170 | + makeEntryVariants(variantUid) |
| 171 | + .delete(variantUid) |
| 172 | + .then((variantEntry) => { |
| 173 | + expect(variantEntry.notice).to.be.equal( |
| 174 | + "Entry variant deleted successfully." |
| 175 | + ); |
| 176 | + done(); |
| 177 | + }) |
| 178 | + .catch(done); |
| 179 | + }); |
| 180 | + |
| 181 | + it("Delete a Variant from uid", (done) => { |
| 182 | + makeVariantGroup(variantGroupUid) |
| 183 | + .variants(variantUid) |
| 184 | + .delete() |
| 185 | + .then((data) => { |
| 186 | + expect(data.message).to.be.equal("Variant deleted successfully"); |
| 187 | + done(); |
| 188 | + }) |
| 189 | + .catch(done); |
| 190 | + }); |
| 191 | + |
| 192 | + it("Delete a Variant Group from uid", (done) => { |
| 193 | + makeVariantGroup(variantGroupUid) |
| 194 | + .delete() |
| 195 | + .then((data) => { |
| 196 | + expect(data.message).to.be.equal( |
| 197 | + "Variant Group and Variants deleted successfully" |
| 198 | + ); |
| 199 | + done(); |
| 200 | + }) |
| 201 | + .catch(done); |
| 202 | + }); |
| 203 | +}); |
| 204 | + |
| 205 | +function makeVariants(uid = null) { |
| 206 | + return client |
| 207 | + .stack({ api_key: process.env.API_KEY }) |
| 208 | + .variantGroup(variantGroupUid) |
| 209 | + .variants(uid); |
| 210 | +} |
| 211 | + |
| 212 | +function makeVariantGroup(uid = null) { |
| 213 | + return client.stack({ api_key: process.env.API_KEY }).variantGroup(uid); |
| 214 | +} |
| 215 | + |
| 216 | +function makeEntryVariants(uid = null) { |
| 217 | + return client |
| 218 | + .stack({ api_key: process.env.API_KEY }) |
| 219 | + .contentType(contentTypeUid) |
| 220 | + .entry(entryUid) |
| 221 | + .variants(uid); |
| 222 | +} |
| 223 | + |
| 224 | +function makeEntry() { |
| 225 | + return client |
| 226 | + .stack({ api_key: process.env.API_KEY }) |
| 227 | + .contentType(contentTypeUid); |
| 228 | +} |
0 commit comments