Skip to content

Commit 1c8bc74

Browse files
committed
fix: 🐛 handles workflow response in object format
1 parent e6580d9 commit 1c8bc74

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

lib/stack/workflow/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,11 @@ export function Workflow (http, data = {}) {
296296
}
297297

298298
export function WorkflowCollection (http, data) {
299-
const obj = cloneDeep(data.workflows) || []
299+
let obj = cloneDeep(data.workflows) || []
300+
if (!Array.isArray(obj)) {
301+
obj = [obj]
302+
}
303+
300304
return obj.map((userData) => {
301305
return new Workflow(http, { workflow: userData, stackHeaders: data.stackHeaders })
302306
})

test/unit/workflow-test.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('Contentstack Workflow test', () => {
8383

8484
it('Workflow Collection test with blank data', done => {
8585
const workflow = new WorkflowCollection(Axios, {})
86-
expect(workflow.length).to.be.equal(0)
86+
expect(workflow[0]).to.be.equal(undefined)
8787
done()
8888
})
8989

@@ -93,7 +93,15 @@ describe('Contentstack Workflow test', () => {
9393
workflowMock
9494
]
9595
})
96-
expect(workflow.length).to.be.equal(1)
96+
expect(typeof workflow[0]).to.be.equal('object')
97+
checkWorkflow(workflow[0])
98+
done()
99+
})
100+
101+
it('Workflow Collection test with data without array', done => {
102+
const workflow = new WorkflowCollection(Axios, {
103+
workflows: workflowMock
104+
})
97105
checkWorkflow(workflow[0])
98106
done()
99107
})
@@ -130,6 +138,20 @@ describe('Contentstack Workflow test', () => {
130138
.catch(done)
131139
})
132140

141+
it('Workflow Fetch all without Stack Headers test with response in object format instead of array of workflows', done => {
142+
var mock = new MockAdapter(Axios)
143+
mock.onGet('/workflows').reply(200, {
144+
workflows: workflowMock
145+
})
146+
makeWorkflow()
147+
.fetchAll()
148+
.then((workflows) => {
149+
checkWorkflow(workflows.items[0])
150+
done()
151+
})
152+
.catch(done)
153+
})
154+
133155
it('Workflow Fetch all with params test', done => {
134156
var mock = new MockAdapter(Axios)
135157
mock.onGet('/workflows').reply(200, {
@@ -269,7 +291,6 @@ describe('Contentstack Workflow test', () => {
269291
.catch(done)
270292
})
271293

272-
273294
it('Workflow content type get publish rules', done => {
274295
var mock = new MockAdapter(Axios)
275296
mock.onGet('/workflows/content_type/ct_UID').reply(200, {
@@ -288,7 +309,6 @@ describe('Contentstack Workflow test', () => {
288309
.catch(done)
289310
})
290311

291-
292312
it('Workflow content type get publish rules', done => {
293313
var mock = new MockAdapter(Axios)
294314
mock.onGet('/workflows/content_type/ct_UID').reply(200, {
@@ -297,7 +317,7 @@ describe('Contentstack Workflow test', () => {
297317
]
298318
})
299319
makeWorkflow().contentType('ct_UID')
300-
.getPublishRules({ action: "publish", locale: "en-us" })
320+
.getPublishRules({ action: 'publish', locale: 'en-us' })
301321
.then((response) => {
302322
checkPublishRules(response.items[0])
303323
done()
@@ -306,10 +326,9 @@ describe('Contentstack Workflow test', () => {
306326
})
307327
})
308328

309-
310329
function makeWorkflow (data) {
311-
return new Workflow(Axios, data)
312-
}
330+
return new Workflow(Axios, data)
331+
}
313332

314333
function checkWorkflow (workflow) {
315334
checkSystemFields(workflow)
@@ -325,7 +344,6 @@ function checkPublishRules (publishRules) {
325344
checkSystemFields(publishRules)
326345
expect(publishRules.locale).to.be.equal('en-us')
327346
expect(publishRules.action).to.be.equal('publish')
328-
expect(publishRules.environment).to.be.equal("env")
329-
expect(publishRules.workflow_stage).to.be.equal("stage")
347+
expect(publishRules.environment).to.be.equal('env')
348+
expect(publishRules.workflow_stage).to.be.equal('stage')
330349
}
331-

0 commit comments

Comments
 (0)