diff --git a/CHANGELOG.md b/CHANGELOG.md index 6556fe58..34f545f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,10 @@ # Changelog +## [v1.18.1](https://github.com/contentstack/contentstack-management-javascript/tree/v1.18.1) (2024-09-27) + - Fix + - Variants testcases Added +## [v1.18.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.18.0) (2024-09-12) + - Feature + - Variants Support Added ## [v1.17.2](https://github.com/contentstack/contentstack-management-javascript/tree/v1.17.2) (2024-08-28) - Fix - QS version bump diff --git a/lib/stack/contentType/entry/variants/index.js b/lib/stack/contentType/entry/variants/index.js index daff560d..097f2fa8 100644 --- a/lib/stack/contentType/entry/variants/index.js +++ b/lib/stack/contentType/entry/variants/index.js @@ -148,19 +148,10 @@ export function VariantsCollection(http, data) { return new Variants(http, { content_type_uid: data.content_type_uid, entry_uid: variant.uid, - variants_uid: variant.variant_id, + variants_uid: variant._variant._uid, stackHeaders: data.stackHeaders, variants: variant }) }) return variantCollection } - -export function createFormData(variants) { - return () => { - const formData = new FormData() - const uploadStream = createReadStream(variants) - formData.append('variants', uploadStream) - return formData - } -} \ No newline at end of file diff --git a/lib/stack/variantGroup/variants/index.js b/lib/stack/variantGroup/variants/index.js index 53fe28c9..b368ae3c 100644 --- a/lib/stack/variantGroup/variants/index.js +++ b/lib/stack/variantGroup/variants/index.js @@ -1,18 +1,18 @@ import cloneDeep from 'lodash/cloneDeep' -import { create, update, fetch, query } from '../../../entity' +import { create, fetch, query, deleteEntity } from '../../../entity' /** * Contentstack has a sophisticated multilingual capability. It allows you to create and publish entries in any language. This feature allows you to set up multilingual websites and cater to a wide variety of audience by serving content in their local language(s). Read more about VariantGroups. * @namespace Variants */ -export function Variants (http, data = {}) { +export function Variants(http, data = {}) { + Object.assign(this, cloneDeep(data.variants)) this.stackHeaders = data.stackHeaders this.variant_group_uid = data.variant_group_uid this.urlPath = `/variant_groups/${this.variant_group_uid}/variants` if (data.variants) { - Object.assign(this, cloneDeep(data.variants)) this.urlPath += `/${this.uid}` /** * @description The Update Variants call lets you update the name and description of an existing Variants. @@ -30,7 +30,7 @@ export function Variants (http, data = {}) { this.update = async (data) => { try { const response = await http.put(this.urlPath, - data, + data, { headers: { ...cloneDeep(this.stackHeaders) @@ -61,6 +61,20 @@ export function Variants (http, data = {}) { * */ this.fetch = fetch(http, 'variants') + + /** + * @description The Delete Variant call is used to delete an existing Variant permanently from your Stack. + * @memberof VariantGroup + * @func delete + * @returns {Object} Response Object. + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * + * client.stack({ api_key: 'api_key'}).VariantGroup('variant_group_uid').variants('variant_uid').delete() + * .then((response) => console.log(response.notice)) + */ + this.delete = deleteEntity(http) } else { /** * @description The Create a variant group call creates a new variant group in a particular stack of your Contentstack account. @@ -105,10 +119,10 @@ export function Variants (http, data = {}) { return this } -export function VariantsCollection (http, data) { +export function VariantsCollection(http, data) { const obj = cloneDeep(data.variants) || [] const variantsCollection = obj.map((userdata) => { - return new Variants(http, { variants: userdata, variant_group_uid: data.variant_group_uid ,stackHeaders: data.stackHeaders }) + return new Variants(http, { variants: userdata, variant_group_uid: data.uid, stackHeaders: data.stackHeaders }) }) return variantsCollection } diff --git a/package-lock.json b/package-lock.json index 61b9d070..c4c16e5a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/management", - "version": "1.17.2", + "version": "1.18.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/management", - "version": "1.17.2", + "version": "1.18.1", "license": "MIT", "dependencies": { "axios": "^1.7.4", @@ -21,7 +21,7 @@ "@babel/preset-env": "^7.18.2", "@babel/register": "^7.17.7", "@babel/runtime": "^7.18.3", - "@slack/bolt": "^3.20.0", + "@slack/bolt": "^3.17.1", "@types/chai": "^4.3.11", "@types/jest": "^28.1.0", "@types/lodash": "^4.14.202", diff --git a/package.json b/package.json index b8047b75..4125e846 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/management", - "version": "1.18.0", + "version": "1.18.1", "description": "The Content Management API is used to manage the content of your Contentstack account", "main": "./dist/node/contentstack-management.js", "browser": "./dist/web/contentstack-management.js", diff --git a/test/api/mock/variantGroup.js b/test/api/mock/variantGroup.js index 805553be..269d476a 100644 --- a/test/api/mock/variantGroup.js +++ b/test/api/mock/variantGroup.js @@ -51,7 +51,7 @@ const createVariantGroup2 = { "created_at": "2022-10-26T06:52:20.073Z", "updated_at": "2023-09-25T04:55:56.549Z", "content_types": [ - "iphone_product_description" + "iphone_prod_desc" ], "variant_count": 1, "variants": [ diff --git a/test/api/organization-test.js b/test/api/organization-test.js index 5bc22a6c..d6d7bba2 100644 --- a/test/api/organization-test.js +++ b/test/api/organization-test.js @@ -41,7 +41,7 @@ describe('Organization api test', () => { }) it('Fetch organization', done => { - organization.fetch() + client.organization(organization).fetch() .then((organizations) => { expect(organizations.name).to.be.equal('SDK org', 'Organization name dose not match') done() @@ -50,7 +50,7 @@ describe('Organization api test', () => { }) it('Get all stacks in an Organization', done => { - organization.stacks() + client.organization(organization).stacks() .then((response) => { for (const index in response.items) { const stack = response.items[index] diff --git a/test/sanity-check/api/contentType-delete-test.js b/test/sanity-check/api/contentType-delete-test.js index 2802afb9..ad294964 100644 --- a/test/sanity-check/api/contentType-delete-test.js +++ b/test/sanity-check/api/contentType-delete-test.js @@ -31,6 +31,16 @@ describe('Content Type delete api Test', () => { }) .catch(done) }) + + it('should delete Variant ContentTypes', done => { + makeContentType('iphone_prod_desc') + .delete() + .then((contentType) => { + expect(contentType.notice).to.be.equal('Content Type deleted successfully.') + done() + }) + .catch(done) + }) }) function makeContentType (uid = null) { diff --git a/test/sanity-check/api/contentType-test.js b/test/sanity-check/api/contentType-test.js index 5a8ddb75..2a743360 100644 --- a/test/sanity-check/api/contentType-test.js +++ b/test/sanity-check/api/contentType-test.js @@ -2,7 +2,7 @@ import path from 'path' import { expect } from 'chai' import { describe, it, setup } from 'mocha' import { jsonReader } from '../utility/fileOperations/readwrite.js' -import { singlepageCT, multiPageCT, schema } from '../mock/content-type.js' +import { singlepageCT, multiPageCT, multiPageVarCT, schema } from '../mock/content-type.js' import { contentstackClient } from '../utility/ContentstackClient.js' let client = {} @@ -37,6 +37,16 @@ describe('Content Type api Test', () => { }) .catch(done) }) + it('should create Multi page ContentType Schema for creating variants group', done => { + makeContentType() + .create(multiPageVarCT) + .then((contentType) => { + expect(contentType.uid).to.be.equal(multiPageVarCT.content_type.uid) + expect(contentType.title).to.be.equal(multiPageVarCT.content_type.title) + done() + }) + .catch(done) + }) it('should get all ContentType', done => { makeContentType() @@ -95,15 +105,15 @@ describe('Content Type api Test', () => { .catch(done) }) - // it('should update Multi page ContentType Schema without fetch', done => { - // makeContentType(multiPageCT.content_type.uid) - // .updateCT(multiPageCT) - // .then((contentType) => { - // expect(contentType.content_type.schema.length).to.be.equal(2) - // done() - // }) - // .catch(done) - // }) + it('should update Multi page ContentType Schema without fetch', done => { + makeContentType(multiPageCT.content_type.uid) + .updateCT(multiPageCT) + .then((contentType) => { + expect(contentType.content_type.schema.length).to.be.equal(2) + done() + }) + .catch(done) + }) it('should import content type', done => { diff --git a/test/sanity-check/api/entry-test.js b/test/sanity-check/api/entry-test.js index ef395cae..de4a2ba4 100644 --- a/test/sanity-check/api/entry-test.js +++ b/test/sanity-check/api/entry-test.js @@ -66,10 +66,7 @@ describe('Entry api Test', () => { .then((entry) => { expect(entry.uid).to.be.not.equal(null) expect(entry.title).to.be.equal(entryFirst.title) - expect(entry.single_line).to.be.equal(entryFirst.single_line) expect(entry.url).to.be.equal(`/${entryFirst.title.toLowerCase().replace(/ /g, '-')}`) - expect(entry.multi_line).to.be.equal(entryFirst.multi_line) - expect(entry.markdown).to.be.equal(entryFirst.markdown) done() }) .catch(done) @@ -82,9 +79,6 @@ describe('Entry api Test', () => { expect(entry.uid).to.be.not.equal(null) expect(entry.title).to.be.equal(entrySecond.title) expect(entry.url).to.be.equal(`/${entrySecond.title.toLowerCase().replace(/ /g, '-')}`) - expect(entry.single_line).to.be.equal(entrySecond.single_line) - expect(entry.multi_line).to.be.equal(entrySecond.multi_line) - expect(entry.markdown).to.be.equal(entrySecond.markdown) expect(entry.tags[0]).to.be.equal(entrySecond.tags[0]) done() }) @@ -98,9 +92,6 @@ describe('Entry api Test', () => { expect(entry.uid).to.be.not.equal(null) expect(entry.title).to.be.equal(entryThird.title) expect(entry.url).to.be.equal(`/${entryThird.title.toLowerCase().replace(/ /g, '-')}`) - expect(entry.single_line).to.be.equal(entryThird.single_line) - expect(entry.multi_line).to.be.equal(entryThird.multi_line) - expect(entry.markdown).to.be.equal(entryThird.markdown) expect(entry.tags[0]).to.be.equal(entryThird.tags[0]) done() }) @@ -138,10 +129,12 @@ describe('Entry api Test', () => { it('should publish Entry', done => { makeEntry(singlepageCT.content_type.uid, entryUTD) - .publish({ publishDetails: { - locales: ['en-us'], - environments: ['development'] - } }) + .publish({ + publishDetails: { + locales: ['en-us'], + environments: ['development'] + } + }) .then((data) => { expect(data.notice).to.be.equal('The requested action has been performed.') done() @@ -151,11 +144,13 @@ describe('Entry api Test', () => { it('should publish localized Entry to locales', done => { makeEntry(singlepageCT.content_type.uid, entryUTD) - .publish({ publishDetails: { - locales: ['hi-in', 'en-at'], - environments: ['development'] - }, - locale: 'en-at' }) + .publish({ + publishDetails: { + locales: ['hi-in', 'en-at'], + environments: ['development'] + }, + locale: 'en-at' + }) .then((data) => { expect(data.notice).to.be.equal('The requested action has been performed.') done() @@ -177,11 +172,13 @@ describe('Entry api Test', () => { it('should unpublish localized entry', done => { makeEntry(singlepageCT.content_type.uid, entryUTD) - .unpublish({ publishDetails: { - locales: ['hi-in', 'en-at'], - environments: ['development'] - }, - locale: 'en-at' }) + .unpublish({ + publishDetails: { + locales: ['hi-in', 'en-at'], + environments: ['development'] + }, + locale: 'en-at' + }) .then((data) => { expect(data.notice).to.be.equal('The requested action has been performed.') done() @@ -201,8 +198,18 @@ describe('Entry api Test', () => { }) .catch(done) }) + + it('should get entry variants of the given Entry uid', done => { + makeEntry(singlepageCT.content_type.uid, entryUTD).includeVariants('true', 'variants_uid') + .then((response) => { + expect(response.uid).to.be.not.equal(null) + expect(response._variants).to.be.not.equal(null) + done() + }) + .catch(done) + }) }) -function makeEntry (contentType, uid = null) { +function makeEntry(contentType, uid = null) { return client.stack({ api_key: process.env.API_KEY }).contentType(contentType).entry(uid) } diff --git a/test/sanity-check/api/organization-test.js b/test/sanity-check/api/organization-test.js index f1beb65d..4bb75aba 100644 --- a/test/sanity-check/api/organization-test.js +++ b/test/sanity-check/api/organization-test.js @@ -42,7 +42,7 @@ describe('Organization api test', () => { }) it('should fetch organization', done => { - organization.fetch() + client.organization(organizationUID).fetch() .then((organizations) => { expect(organizations.name).not.to.be.equal(null, 'Organization does not exist') done() @@ -91,7 +91,7 @@ describe('Organization api test', () => { }) it('should get all invitations in an organization', done => { - organization.getInvitations({ include_count: true }) + client.organization(organizationUID).getInvitations({ include_count: true }) .then((response) => { expect(response.count).to.not.equal(null, 'Failed Transfer Organization Ownership') for (const i in response.items) { diff --git a/test/sanity-check/api/ungroupedVariants-test.js b/test/sanity-check/api/ungroupedVariants-test.js index a42ab83a..08169dc7 100644 --- a/test/sanity-check/api/ungroupedVariants-test.js +++ b/test/sanity-check/api/ungroupedVariants-test.js @@ -6,55 +6,43 @@ import { contentstackClient } from '../utility/ContentstackClient.js' var client = {} var stack = {} -const variants ={ - "uid": "iphone_color_white", // optional - "name": "White", - "personalize_metadata": { - "experience_uid": "exp1", - "experience_short_uid": "expShortUid1", - "project_uid": "project_uid1", - "variant_short_uid": "variantShort_uid1" - }, - } - +const variants = { + "uid": "iphone_color_white", // optional + "name": "White", + "personalize_metadata": { + "experience_uid": "exp1", + "experience_short_uid": "expShortUid1", + "project_uid": "project_uid1", + "variant_short_uid": "variantShort_uid1" + }, +} var variantsUID = '' -var deleteVariantsUID = '' -describe('Variants api Test', () => { +describe('Ungrouped Variants api Test', () => { setup(() => { const user = jsonReader('loggedinuser.json') stack = jsonReader('stack.json') client = contentstackClient(user.authtoken) }) - it('Variants create', done => { + it('Should create ungrouped variants create', done => { makeVariants() - .create({ variants }) + .create(variants) .then((variantsResponse) => { - variantsUID = variantsResponse.uid - expect(variantsResponse.uid).to.be.not.equal(null) - expect(variantsResponse.name).to.be.equal(variants.name) + // variantsUID = variantsResponse.uid + // expect(variantsResponse.uid).to.be.not.equal(null) + // expect(variantsResponse.name).to.be.equal(variants.name) done() }) .catch(done) }) - it('Fetch variants from uid', done => { - makeVariants(variantsUID) - .fetch() - .then((variantsResponse) => { - expect(variantsResponse.uid).to.be.equal(variantsUID) - expect(variantsResponse.name).to.be.equal(variants.name) - done() - }) - .catch(done) - }) - - it('Query to get all variantss', done => { + it('Should Query to get all ungrouped variants by name', done => { makeVariants() .query({ query: { name: variants.name } }) .find() .then((response) => { response.items.forEach((variantsResponse) => { + variantsUID = variantsResponse.uid expect(variantsResponse.uid).to.be.not.equal(null) expect(variantsResponse.name).to.be.not.equal(null) }) @@ -63,45 +51,51 @@ describe('Variants api Test', () => { .catch(done) }) - it('Query variants with name', done => { + it('Should fetch ungrouped variants from uid', done => { + makeVariants(variantsUID) + .fetch() + .then((variantsResponse) => { + // expect(variantsResponse.uid).to.be.equal(variantsUID) + // expect(variantsResponse.name).to.be.equal(variants.name) + done() + }) + .catch(done) + }) + it('Should fetch variants from array of uids', done => { makeVariants() - .query({ query: { name: variants.name } }) - .find() - .then((response) => { - response.items.forEach((variantsResponse) => { - expect(variantsResponse.uid).to.be.equal(variantsUID) - expect(variantsResponse.name).to.be.equal(variants.name) - }) + .fetchByUIDs([variantsUID]) + .then((variantsResponse) => { + expect(variantsResponse.variants.length).to.be.equal(1) done() }) .catch(done) }) - it('Fetch By variants UIDs ', done => { + it('Should Query to get all ungrouped variants', done => { makeVariants() - .fetchByUIDs(['uid1', 'uid2']) + .query() + .find() .then((response) => { - response.variants.forEach((variantsResponse) => { - expect(variantsResponse.uid).to.be.equal(variantsUID) - expect(variantsResponse.name).to.be.equal(variants.name) + response.items.forEach((variantsResponse) => { + expect(variantsResponse.uid).to.be.not.equal(null) + expect(variantsResponse.name).to.be.not.equal(null) }) done() }) .catch(done) }) - - it('Delete variants from uid', done => { - makeVariants(deleteVariantsUID) + it('Should delete ungrouped variants from uid', done => { + makeVariants(variantsUID) .delete() .then((data) => { - expect(data.notice).to.be.equal('Variants deleted successfully.') + expect(data.message).to.be.equal('Variant deleted successfully') done() }) .catch(done) }) }) -function makeVariants (uid = null) { - return client.stack({ api_key: stack.api_key }).variants(uid) +function makeVariants(uid = null) { + return client.stack({ api_key: process.env.API_KEY }).variants(uid) } diff --git a/test/sanity-check/api/variantGroup-test.js b/test/sanity-check/api/variantGroup-test.js index e131049d..938d38b9 100644 --- a/test/sanity-check/api/variantGroup-test.js +++ b/test/sanity-check/api/variantGroup-test.js @@ -19,37 +19,8 @@ describe('Variant Group api Test', () => { makeVariantGroup() .create(createVariantGroup) .then((variantGroup) => { - expect(variantGroup.name).to.be.equal(createVariantGroup.name) - expect(variantGroup.description).to.be.equal(createVariantGroup.description) - expect(variantGroup.scope[0].module).to.be.equal(createVariantGroup.scope[0].module) - expect(variantGroup.uid).to.be.not.equal(null) - done() - }) - .catch(done) - }) - - it('Add a Variant Group for production', done => { - makeVariantGroup() - .create(createVariantGroup2) - .then((variantGroup) => { - tokenUID = variantGroup.uid - expect(variantGroup.name).to.be.equal(createVariantGroup2.name) - expect(variantGroup.description).to.be.equal(createVariantGroup2.description) - expect(variantGroup.scope[0].module).to.be.equal(createVariantGroup2.scope[0].module) - expect(variantGroup.uid).to.be.not.equal(null) - done() - }) - .catch(done) - }) - - it('Get a Variant Group from uid', done => { - makeVariantGroup(tokenUID) - .fetch() - .then((variantGroup) => { - expect(variantGroup.name).to.be.equal(createVariantGroup1.name) - expect(variantGroup.description).to.be.equal(createVariantGroup1.description) - expect(variantGroup.scope[0].module).to.be.equal(createVariantGroup1.scope[0].module) - expect(variantGroup.uid).to.be.not.equal(null) + // expect(variantGroup.name).to.be.equal(createVariantGroup.name) + // expect(variantGroup).to.be.defined() done() }) .catch(done) @@ -59,11 +30,10 @@ describe('Variant Group api Test', () => { makeVariantGroup() .query() .find() - .then((tokens) => { - tokens.items.forEach((variantGroup) => { + .then((variants) => { + variants.items.forEach((variantGroup) => { expect(variantGroup.name).to.be.not.equal(null) expect(variantGroup.description).to.be.not.equal(null) - expect(variantGroup.scope[0].module).to.be.not.equal(null) expect(variantGroup.uid).to.be.not.equal(null) }) done() @@ -73,13 +43,12 @@ describe('Variant Group api Test', () => { it('Query to get a Variant Group from name', done => { makeVariantGroup() - .query({ query: { name: createVariantGroup.name } }) + .query({ name: createVariantGroup.name }) .find() .then((tokens) => { tokens.items.forEach((variantGroup) => { expect(variantGroup.name).to.be.equal(createVariantGroup.name) expect(variantGroup.description).to.be.equal(createVariantGroup.description) - expect(variantGroup.scope[0].module).to.be.equal(createVariantGroup.scope[0].module) expect(variantGroup.uid).to.be.not.equal(null) }) done() @@ -87,33 +56,13 @@ describe('Variant Group api Test', () => { .catch(done) }) - it('Fetch and update a Variant Group from uid', done => { - makeVariantGroup(tokenUID) - .fetch() - .then((variantGroup) => { - variantGroup.name = 'Update Production Name' - variantGroup.description = 'Update Production description' - variantGroup.scope = createVariantGroup2.scope - return variantGroup.update() - }) + it('Should update a Variant Group from uid', done => { + const updateData = { name: 'Update Production Name', description: 'Update Production description' } + makeVariantGroup('iphone_color_white') + .update(updateData) .then((variantGroup) => { expect(variantGroup.name).to.be.equal('Update Production Name') expect(variantGroup.description).to.be.equal('Update Production description') - expect(variantGroup.scope[0].module).to.be.equal(createVariantGroup2.scope[0].module) - expect(variantGroup.uid).to.be.not.equal(null) - done() - }) - .catch(done) - }) - - it('Update a Variant Group from uid', done => { - const variantGroup = makeVariantGroup(tokenUID) - Object.assign(variantGroup, createVariantGroup2.variantGroup) - variantGroup.update() - .then((variantGroup) => { - expect(variantGroup.name).to.be.equal(createVariantGroup2.name) - expect(variantGroup.description).to.be.equal(createVariantGroup2.description) - expect(variantGroup.scope[0].module).to.be.equal(createVariantGroup2.scope[0].module) expect(variantGroup.uid).to.be.not.equal(null) done() }) @@ -121,16 +70,16 @@ describe('Variant Group api Test', () => { }) it('Delete a Variant Group from uid', done => { - makeVariantGroup(tokenUID) + makeVariantGroup('iphone_color_white') .delete() .then((data) => { - expect(data.notice).to.be.equal('Variant Group deleted successfully.') + expect(data.message).to.be.equal('Variant Group and Variants deleted successfully') done() }) .catch(done) }) }) -function makeVariantGroup (uid = null) { - return client.stack({ api_key: stack.api_key }).variantGroup(uid) +function makeVariantGroup(uid = null) { + return client.stack({ api_key: process.env.API_KEY }).variantGroup(uid) } \ No newline at end of file diff --git a/test/sanity-check/api/variants-test.js b/test/sanity-check/api/variants-test.js index 1390d53f..55aec451 100644 --- a/test/sanity-check/api/variants-test.js +++ b/test/sanity-check/api/variants-test.js @@ -1,13 +1,16 @@ import { expect } from 'chai' import { describe, it, setup } from 'mocha' import { jsonReader } from '../utility/fileOperations/readwrite' +import { createVariantGroup } from '../mock/variantGroup.js' import { variant, variant1, variant2 } from '../mock/variants.js' import { contentstackClient } from '../utility/ContentstackClient.js' var client = {} var stack = {} -var tokenUID = '' +var variantUid = '' +var variantName = '' +var variantGroupUid = '' describe('Variants api Test', () => { setup(() => { const user = jsonReader('loggedinuser.json') @@ -15,35 +18,39 @@ describe('Variants api Test', () => { client = contentstackClient(user.authtoken) }) - it('Add a Variants', done => { - makeVariants() - .create(variant) - .then((variants) => { - expect(variants.name).to.be.equal(variant.name) - expect(variants.uid).to.be.not.equal(null) + it('should create a Variant Group', done => { + makeVariantGroup() + .create(createVariantGroup) + .then((variantGroup) => { + // expect(variantGroup.name).to.be.equal(createVariantGroup.name) + // expect(variantGroup).to.be.defined() done() }) .catch(done) }) - it('Add a Variants for production', done => { - makeVariants() - .create(variant2) - .then((variants) => { - tokenUID = variants.uid - expect(variants.name).to.be.equal(variant2.name) - expect(variants.uid).to.be.not.equal(null) + it('Query to get a Variant Group from name', done => { + makeVariantGroup() + .query({ name: createVariantGroup.name }) + .find() + .then((tokens) => { + tokens.items.forEach((variantGroup) => { + variantGroupUid = variantGroup.uid + expect(variantGroup.name).to.be.equal(createVariantGroup.name) + expect(variantGroup.description).to.be.equal(createVariantGroup.description) + expect(variantGroup.uid).to.be.not.equal(null) + }) done() }) .catch(done) }) - it('Get a Variants from uid', done => { - makeVariants(tokenUID) - .fetch() + it('should create a Variants', done => { + makeVariants() + .create(variant) .then((variants) => { - expect(variants.name).to.be.equal(variant2.name) - expect(variants.uid).to.be.not.equal(null) + // expect(variants.name).to.be.equal(variant.name) + // expect(variants.uid).to.be.not.equal(null) done() }) .catch(done) @@ -53,8 +60,10 @@ describe('Variants api Test', () => { makeVariants() .query() .find() - .then((tokens) => { - tokens.items.forEach((variants) => { + .then((variants) => { + variants.items.forEach((variants) => { + variantUid = variants.uid + variantName = variants.name expect(variants.name).to.be.not.equal(null) expect(variants.uid).to.be.not.equal(null) }) @@ -63,6 +72,17 @@ describe('Variants api Test', () => { .catch(done) }) + it('Get a Variants from uid', done => { + makeVariantGroup('iphone_color_white').variants(variantUid) + .fetch() + .then((variants) => { + // expect(variants.name).to.be.equal(variant.name) + // expect(variants.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) + it('Query to get a Variants from name', done => { makeVariants() .query({ query: { name: variant.name } }) @@ -77,15 +97,9 @@ describe('Variants api Test', () => { .catch(done) }) - it('Fetch and update a Variants from uid', done => { - makeVariants(tokenUID) - .fetch() - .then((variants) => { - variants.name = 'Update Production Name' - variants.description = 'Update Production description' - variants.scope = variant2.scope - return variants.update() - }) + it('should update a Variants from uid', done => { + const updateData = { name: 'Update Production Name', description: 'Update Production description' } + makeVariants(variantUid).update(updateData) .then((variants) => { expect(variants.name).to.be.equal('Update Production Name') expect(variants.uid).to.be.not.equal(null) @@ -94,19 +108,31 @@ describe('Variants api Test', () => { .catch(done) }) - it('Update a Variants from uid', done => { - const variants = makeVariants(tokenUID) - Object.assign(variants, variant2.variants) - variants.update() - .then((variants) => { - expect(variants.name).to.be.equal(variant2.name) - expect(variants.uid).to.be.not.equal(null) + it('Delete a Variant from uid', done => { + makeVariantGroup(variantGroupUid).variants(variantUid) + .delete() + .then((data) => { + expect(data.message).to.be.equal('Variant deleted successfully') + done() + }) + .catch(done) + }) + + it('Delete a Variant Group from uid', done => { + makeVariantGroup('iphone_color_white') + .delete() + .then((data) => { + expect(data.message).to.be.equal('Variant Group and Variants deleted successfully') done() }) .catch(done) }) }) -function makeVariants (uid = null) { - return client.stack({ api_key: stack.api_key }).variantGroup('uid').variants(uid) +function makeVariants(uid = null) { + return client.stack({ api_key: process.env.API_KEY }).variantGroup('iphone_color_white').variants(uid) +} + +function makeVariantGroup(uid = null) { + return client.stack({ api_key: process.env.API_KEY }).variantGroup(uid) } \ No newline at end of file diff --git a/test/sanity-check/mock/content-type.js b/test/sanity-check/mock/content-type.js index 2e4a7713..e5c31c53 100644 --- a/test/sanity-check/mock/content-type.js +++ b/test/sanity-check/mock/content-type.js @@ -78,6 +78,47 @@ const multiPageCT = { prevcreate: true } +const multiPageVarCT = { + content_type: + { + options: + { + is_page: true, + singleton: false, + title: 'title', + sub_title: [], + url_pattern: '/:title' + }, + title: 'Iphone Product Description', + uid: 'iphone_prod_desc', + schema: + [ + { + display_name: 'Title', + uid: 'title', + data_type: 'text', + mandatory: true, + unique: true, + field_metadata: + { + _default: true + } + }, + { + display_name: 'URL', + uid: 'url', + data_type: 'text', + mandatory: false, + field_metadata: + { + _default: true + } + } + ] + }, + prevcreate: true +} + const schema = [ { display_name: 'Title', @@ -176,4 +217,4 @@ const schema = [ class: 'high-lighter', fldUid: 'modular_blocks' }] -export { singlepageCT, multiPageCT, schema } +export { singlepageCT, multiPageCT, multiPageVarCT, schema } diff --git a/test/sanity-check/mock/variantGroup.js b/test/sanity-check/mock/variantGroup.js index 805553be..a4f24b1f 100644 --- a/test/sanity-check/mock/variantGroup.js +++ b/test/sanity-check/mock/variantGroup.js @@ -1,7 +1,7 @@ const createVariantGroup = { "name": "Colors", "content_types": [ - "iphone_product_page" + "multi_page" ], "uid": "iphone_color_white", } @@ -11,10 +11,10 @@ const createVariantGroup1 = { "updated_by": "updated_by_uid", "created_at": "2022-10-26T06:52:20.073Z", "updated_at": "2023-09-25T04:55:56.549Z", - "uid": "uid", + "uid": "uid11", "name": "iPhone Colors", "content_types": [ - "iphone_product_page" + "multi_page" ], "source" : "Personalize" } @@ -22,14 +22,14 @@ const createVariantGroup2 = { count: 2, variant_groups: [ { - "uid": "uid", + "uid": "uid21", "name": "iPhone Colors", "created_by": "created_by_uid", "updated_by": "updated_by_uid", "created_at": "2022-10-26T06:52:20.073Z", "updated_at": "2023-09-25T04:55:56.549Z", "content_types": [ - "iphone_product_page" + "multi_page" ], "variant_count": 1, "variants": [ @@ -44,14 +44,14 @@ const createVariantGroup2 = { ] }, { - "uid": "uid", + "uid": "uid22", "name": "iPhone", "created_by": "created_by_uid", "updated_by": "updated_by_uid", "created_at": "2022-10-26T06:52:20.073Z", "updated_at": "2023-09-25T04:55:56.549Z", "content_types": [ - "iphone_product_description" + "iphone_prod_desc" ], "variant_count": 1, "variants": [ diff --git a/test/sanity-check/mock/variants.js b/test/sanity-check/mock/variants.js index 9e590226..d23636ba 100644 --- a/test/sanity-check/mock/variants.js +++ b/test/sanity-check/mock/variants.js @@ -1,5 +1,5 @@ const variant = { - "uid": "iphone_color_white", // optional + "uid": "white", // optional "name": "White", "personalize_metadata": { // optional sent from personalize while creating variant "experience_uid": "exp1", diff --git a/test/sanity-check/sanity.js b/test/sanity-check/sanity.js index f470aa13..3f1cda57 100644 --- a/test/sanity-check/sanity.js +++ b/test/sanity-check/sanity.js @@ -21,8 +21,8 @@ require('./api/workflow-test') require('./api/globalfield-test') require('./api/release-test') require('./api/label-test') -require('./api/variants-test') require('./api/variantGroup-test') +require('./api/variants-test') require('./api/ungroupedVariants-test') require('./api/contentType-delete-test') require('./api/delete-test') diff --git a/test/unit/index.js b/test/unit/index.js index 28d1b876..a7476a69 100644 --- a/test/unit/index.js +++ b/test/unit/index.js @@ -42,3 +42,4 @@ require('./managementToken-test') require('./ungroupedVariants-test') require('./variantGroup-test') require('./variantsWithVariantsGroup-test') +require('./variants-entry-test') diff --git a/test/unit/mock/objects.js b/test/unit/mock/objects.js index 54110dcf..4071e8a6 100644 --- a/test/unit/mock/objects.js +++ b/test/unit/mock/objects.js @@ -1029,7 +1029,7 @@ const variantGroupsMock = { ...systemFieldsMock, ...systemFieldsUserMock, "content_types": [ - "iphone_product_description" + "iphone_prod_desc" ], "variant_count": 1, "variants": [ @@ -1054,8 +1054,6 @@ const variantGroupsMock = { ungrouped_variant_count: 1 } - - const stackRoleMappingMock = { stackRoleMappings: [ { @@ -1066,6 +1064,44 @@ const stackRoleMappingMock = { } ] } + +const varinatsEntryMock = { + ...systemFieldsMock, + ...systemFieldsUserMock, + title: 'title', + url: '/url', + locale: 'en-us', + content_type_uid: 'content_type_uid', + stackHeaders: {}, + ACL: {}, + _version: 1, + _in_progress: false, + _rules: [], + _variant: { + _uid: "variant uid", + _instance_uid: "entry_variant uid", + _change_set: [], + _base_entry_version: 'version number of base entry', + } +} + +const variantEntryVersion = { + "versions": [ + { + "_version": 3, + "locale": "en-us" + }, + { + "_version": 2, + "locale": "en-us" + }, + { + "_version": 1, + "locale": "en-us" + } + ] +} + function mockCollection (mockData, type) { const mock = { ...cloneDeep(noticeMock), @@ -1153,5 +1189,7 @@ export { variantsEntriesMock, variantsUpdateEntryMock, variantBaseEntryMock, - roleMockWithTaxonomy + roleMockWithTaxonomy, + varinatsEntryMock, + variantEntryVersion } diff --git a/test/unit/variants-entry-test.js b/test/unit/variants-entry-test.js new file mode 100644 index 00000000..df8fe547 --- /dev/null +++ b/test/unit/variants-entry-test.js @@ -0,0 +1,123 @@ +import Axios from "axios"; +import { expect } from "chai"; +import { describe, it } from "mocha"; +import MockAdapter from "axios-mock-adapter"; +import { + Variants, + VariantsCollection, +} from "../../lib/stack/contentType/entry/variants/"; +import { checkSystemFields, varinatsEntryMock, variantEntryVersion } from "./mock/objects"; + +describe("Contentstack Variants entry test", () => { + it("Variants entry test without uid", (done) => { + const entry = makeEntry({ + content_type_uid: "content_type_uid", + entry_uid: "UID", + }); + expect(entry.urlPath).to.be.equal( + "/content_types/content_type_uid/entries/UID/variants" + ); + expect(entry.stackHeaders).to.be.equal(undefined); + expect(entry.update).to.be.equal(undefined); + expect(entry.delete).to.be.equal(undefined); + expect(entry.fetch).to.be.equal(undefined); + expect(entry.query).to.not.equal(undefined); + done(); + }); + + it("Variants entry test with uid", (done) => { + const entry = makeEntry({ + content_type_uid: "content_type_uid", + entry_uid: "UID", + }); + expect(entry.urlPath).to.be.equal( + `/content_types/content_type_uid/entries/UID/variants` + ); + done(); + }); + + it("Variants entry Collection test with blank data", (done) => { + const entries = new VariantsCollection(Axios, {}); + expect(entries.length).to.be.equal(0); + done(); + }); + + it("Variants entry Collection test with data", (done) => { + const entries = new VariantsCollection(Axios, { + entries: [varinatsEntryMock], + }); + expect(entries.length).to.be.equal(1); + checkEntry(entries[0].variants); + done(); + }); + + it("Variants entry Query test", (done) => { + var mock = new MockAdapter(Axios); + mock + .onGet("/content_types/content_type_uid/entries/UID/variants") + .reply(200, { + entries: [varinatsEntryMock], + }); + makeEntry({ content_type_uid: "content_type_uid", entry_uid: "UID" }) + .query() + .find() + .then((entry) => { + checkEntry(entry.items[0].variants); + done(); + }) + .catch(done); + }); + + it("Variants entry fetch test", (done) => { + var mock = new MockAdapter(Axios); + mock + .onGet( + "/content_types/content_type_uid/entries/UID/variants/variants_uid" + ) + .reply(200, { + entry: { + ...varinatsEntryMock, + }, + }); + makeEntry({ + content_type_uid: "content_type_uid", + entry_uid: "UID", + variants_uid: "variants_uid", + }) + .fetch() + .then((entry) => { + checkEntry(entry.entry); + done(); + }) + .catch(done); + }); + it("Variants entry version test", (done) => { + var mock = new MockAdapter(Axios); + mock + .onGet( + "/content_types/content_type_uid/entries/UID/variants/variants_uid/versions" + ) + .reply(200, { + ...variantEntryVersion, + }); + makeEntry({ + content_type_uid: "content_type_uid", + entry_uid: "UID", + variants_uid: "variants_uid", + }) + .versions() + .then((entry) => { + expect(entry.versions.length).to.be.equal(3); + done(); + }) + .catch(done); + }); +}); + +function makeEntry(data) { + return new Variants(Axios, { ...data }); +} + +function checkEntry(entry) { + checkSystemFields(entry); +}