1+ import Axios from 'axios' ;
2+ import { expect } from 'chai' ;
3+ import MockAdapter from 'axios-mock-adapter' ;
4+ import { describe , it } from 'mocha' ;
5+ import { BulkOperation } from '../../lib/stack/bulkOperation' ;
6+ import { stackHeadersMock } from './mock/objects' ;
7+
8+ describe ( 'Contentstack BulkOperation test' , ( ) => {
9+ it ( 'BulkOperation test without uid' , done => {
10+ const bulkOperation = makeBulkOperation ( ) ;
11+ expect ( bulkOperation . urlPath ) . to . be . equal ( '/bulk' ) ;
12+ expect ( bulkOperation . stackHeaders ) . to . be . equal ( undefined ) ;
13+ expect ( bulkOperation . addItems ) . to . not . equal ( undefined ) ;
14+ expect ( bulkOperation . publish ) . to . not . equal ( undefined ) ;
15+ expect ( bulkOperation . unpublish ) . to . not . equal ( undefined ) ;
16+ expect ( bulkOperation . delete ) . to . not . equal ( undefined ) ;
17+ done ( ) ;
18+ } ) ;
19+
20+ it ( 'BulkOperation test with stackHeaders' , done => {
21+ const bulkOperation = makeBulkOperation ( { stackHeaders : { ...stackHeadersMock } } ) ;
22+ expect ( bulkOperation . urlPath ) . to . be . equal ( '/bulk' ) ;
23+ expect ( bulkOperation . stackHeaders ) . to . not . equal ( undefined ) ;
24+ expect ( bulkOperation . stackHeaders . api_key ) . to . be . equal ( stackHeadersMock . api_key ) ;
25+ expect ( bulkOperation . addItems ) . to . not . equal ( undefined ) ;
26+ expect ( bulkOperation . publish ) . to . not . equal ( undefined ) ;
27+ expect ( bulkOperation . unpublish ) . to . not . equal ( undefined ) ;
28+ expect ( bulkOperation . delete ) . to . not . equal ( undefined ) ;
29+ done ( ) ;
30+ } ) ;
31+
32+ it ( 'should add items to a release' , async ( ) => {
33+ const items = {
34+ release : 'blt05e951e5f3a1d342' ,
35+ action : 'publish' ,
36+ locale : [ 'en-us' ] ,
37+ reference : true ,
38+ items : [
39+ {
40+ content_type_uid : 'ct_1' ,
41+ uid : 'bltf6e197a18a11ec5f' ,
42+ version : 2 ,
43+ locale : 'en-us' ,
44+ title : 'validation test' ,
45+ } ,
46+ ] ,
47+ } ;
48+
49+ var mock = new MockAdapter ( Axios ) ;
50+ mock . onPost ( '/bulk/release/items' ) . reply ( 200 , {
51+ notice : 'Your add to release request is in progress.' ,
52+ job_id : 'job_id' ,
53+ } ) ;
54+
55+ const response = await makeBulkOperation ( ) . addItems ( { data : items , bulk_version : '2.0' } ) ;
56+ expect ( response . notice ) . to . equal ( 'Your add to release request is in progress.' ) ;
57+ expect ( response . job_id ) . to . not . equal ( undefined ) ;
58+ } ) ;
59+
60+ it ( 'should publish items in bulk' , async ( ) => {
61+ const publishDetails = {
62+ entries : [
63+ {
64+ uid : 'entry_uid' ,
65+ content_type : 'content_type_uid' ,
66+ version : 'version' ,
67+ locale : 'entry_locale' ,
68+ } ,
69+ ] ,
70+ assets : [ { uid : 'uid' } ] ,
71+ locales : [ 'en' ] ,
72+ environments : [ 'env_uid' ] ,
73+ } ;
74+
75+ var mock = new MockAdapter ( Axios ) ;
76+ mock . onPost ( '/bulk/publish' ) . reply ( 200 , {
77+ notice : 'Your publish request is in progress.' ,
78+ job_id : 'job_id' ,
79+ } ) ;
80+
81+ const response = await makeBulkOperation ( ) . publish ( { details : publishDetails } ) ;
82+ expect ( response . notice ) . to . equal ( 'Your publish request is in progress.' ) ;
83+ expect ( response . job_id ) . to . not . equal ( undefined ) ;
84+ } ) ;
85+
86+ it ( 'should unpublish items in bulk' , async ( ) => {
87+ const unpublishDetails = {
88+ entries : [
89+ {
90+ uid : 'entry_uid' ,
91+ content_type : 'content_type_uid' ,
92+ version : 'version' ,
93+ locale : 'entry_locale' ,
94+ } ,
95+ ] ,
96+ assets : [ { uid : 'uid' } ] ,
97+ locales : [ 'en' ] ,
98+ environments : [ 'env_uid' ] ,
99+ } ;
100+
101+ var mock = new MockAdapter ( Axios ) ;
102+ mock . onPost ( '/bulk/unpublish' ) . reply ( 200 , {
103+ notice : 'Your unpublish request is in progress.' ,
104+ job_id : 'job_id' ,
105+ } ) ;
106+
107+ const response = await makeBulkOperation ( ) . unpublish ( { details : unpublishDetails } ) ;
108+ expect ( response . notice ) . to . equal ( 'Your unpublish request is in progress.' ) ;
109+ expect ( response . job_id ) . to . not . equal ( undefined ) ;
110+ } ) ;
111+
112+ it ( 'should delete items in bulk' , async ( ) => {
113+ const deleteDetails = {
114+ entries : [
115+ {
116+ uid : 'entry_uid' ,
117+ content_type : 'content_type_uid' ,
118+ locale : 'entry_locale' ,
119+ } ,
120+ ] ,
121+ assets : [ { uid : 'uid' } ] ,
122+ } ;
123+
124+ var mock = new MockAdapter ( Axios ) ;
125+ mock . onPost ( '/bulk/delete' ) . reply ( 200 , {
126+ notice : 'Your delete request is in progress.' ,
127+ job_id : 'job_id' ,
128+ } ) ;
129+
130+ const response = await makeBulkOperation ( ) . delete ( { details : deleteDetails } ) ;
131+ expect ( response . notice ) . to . equal ( 'Your delete request is in progress.' ) ;
132+ expect ( response . job_id ) . to . not . equal ( undefined ) ;
133+ } ) ;
134+
135+ it ( 'should update items in bulk' , async ( ) => {
136+ const updateBody = {
137+ entries : [
138+ {
139+ content_type : 'content_type_uid1' ,
140+ uid : 'entry_uid' ,
141+ locale : 'en-us' ,
142+ } ,
143+ {
144+ content_type : 'content_type_uid2' ,
145+ uid : 'entry_uid' ,
146+ locale : 'en-us' ,
147+ } ,
148+ ] ,
149+ workflow : {
150+ workflow_stage : {
151+ comment : 'Workflow-related Comments' ,
152+ due_date : 'Thu Dec 01 2018' ,
153+ notify : false ,
154+ uid : 'workflow_stage_uid' ,
155+ assigned_to : [
156+ {
157+ uid : 'user_uid' ,
158+ name : 'user_name' ,
159+ email : 'user_email_id' ,
160+ } ,
161+ ] ,
162+ assigned_by_roles : [
163+ {
164+ uid : 'role_uid' ,
165+ name : 'role_name' ,
166+ } ,
167+ ] ,
168+ } ,
169+ } ,
170+ } ;
171+
172+ var mock = new MockAdapter ( Axios ) ;
173+ mock . onPost ( '/bulk/workflow' ) . reply ( 200 , {
174+ notice : 'Your update request is in progress.' ,
175+ job_id : 'job_id' ,
176+ } ) ;
177+
178+ const response = await makeBulkOperation ( ) . update ( updateBody ) ;
179+ expect ( response . notice ) . to . equal ( 'Your update request is in progress.' ) ;
180+ expect ( response . job_id ) . to . not . equal ( undefined ) ;
181+ } ) ;
182+ } ) ;
183+
184+ function makeBulkOperation ( data ) {
185+ return new BulkOperation ( Axios , data ) ;
186+ }
0 commit comments