Skip to content

Commit

Permalink
chore: store merge (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep authored Dec 25, 2024
1 parent 7fafd38 commit c05f573
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pactum",
"version": "3.7.5",
"version": "3.7.6",
"description": "REST API Testing Tool for all levels in a Test Pyramid",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down
7 changes: 6 additions & 1 deletion src/exports/handler.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@ interface WaitHandlerContext extends RequestResponseContext {
rootData?: any;
}

interface ResponseHandlerContext extends RequestResponseContext {
spec: Spec
}

export type SpecHandlerFunction = (ctx: SpecHandlerContext) => void;
export type ExpectHandlerFunction = (ctx: ExpectHandlerContext) => void;
export type RetryHandlerFunction = (ctx: RequestResponseContext) => boolean;
export type ResponseHandlerFunction = (ctx: ResponseHandlerContext) => void;
export type CaptureHandlerFunction = (ctx: CaptureContext) => any;
export type StateHandlerFunction = (ctx: StateHandlerContext) => any;
export type DataHandlerFunction = (ctx: DataHandlerContext) => any;
Expand Down Expand Up @@ -130,4 +135,4 @@ export function addWaitHandler(name: string, func: WaitHandlerFunction): void;
* adds a response handler
* @see https://pactumjs.github.io/api/handlers/addResponseHandler.html
*/
export function addResponseHandler(name: string, func: RetryHandlerFunction): void;
export function addResponseHandler(name: string, func: ResponseHandlerFunction): void;
15 changes: 10 additions & 5 deletions src/helpers/toss.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,19 @@ function storeSpecData(spec, stores) {
} else {
data_to_store = getPathValueFromRequestResponse(store.path, spec._request, spec._response);
}
if(store.options && store.options.append) {
if (store.options && store.options.append) {
ctx.store[store.name] = ctx.store[store.name] || [];
ctx.store[store.name].push(data_to_store);
continue;
}
const specData = {};
specData[store.name] = data_to_store;
stash.addDataStore(specData);
if (store.options && store.options.merge) {
ctx.store[store.name] = ctx.store[store.name] || {};
Object.assign(ctx.store[store.name], data_to_store);
} else {
const specData = {};
specData[store.name] = data_to_store;
stash.addDataStore(specData);
}
}
}

Expand Down Expand Up @@ -147,7 +152,7 @@ function storeInteractionData(request, interaction) {

async function runResponseHandler(spec) {
try {
const ctx = { req: spec._request, res: spec._response };
const ctx = { req: spec._request, res: spec._response, spec };
const handlers = spec._response_handlers;
for (let i = 0; i < handlers.length; i++) {
await hr.response(handlers[i], ctx);
Expand Down
1 change: 1 addition & 0 deletions src/internal.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export type ISpecStore = {
export type ISpecStoreOptions = {
status?: 'PASSED' | 'FAILED'
append?: boolean
merge?: boolean
}
40 changes: 34 additions & 6 deletions test/component/stores.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ const { addInteractionHandler } = pactum.handler;
describe('Stores', () => {

before(() => {
addInteractionHandler('get stores', () => {
addInteractionHandler('get stores', ({ data }) => {
return {
request: {
method: 'GET',
path: '/api/stores'
},
response: {
status: 200,
body: {
id: 1
}
body: data ? data : { id: 1 }
}
};
});
Expand All @@ -30,7 +28,10 @@ describe('Stores', () => {
}
},
response: {
status: 200
status: 200,
body: {
name: 'Snow'
}
}
};
});
Expand Down Expand Up @@ -359,7 +360,34 @@ describe('Stores', () => {
.get('http://localhost:9393/api/stores')
.expectStatus(200)
.stores('UserId', 'id', { append: true });
expect(stash.getDataStore('UserId')).deep.equals([1,1]);
expect(stash.getDataStore('UserId')).deep.equals([1, 1]);
});

it('store merge', async () => {
await pactum.spec()
.useInteraction('get stores', { id: 1 })
.get('http://localhost:9393/api/stores')
.expectStatus(200)
.stores('User', '.');
await pactum.spec()
.useInteraction('get stores', { name: 'Snow' })
.get('http://localhost:9393/api/stores')
.expectStatus(200)
.stores('User', '.', { merge: true });
expect(stash.getDataStore()).deep.equals({ User: { id: 1, name: 'Snow' } });
});

it('store merge in same request', async () => {
await pactum.spec()
.useInteraction('post stores')
.post('http://localhost:9393/api/stores')
.withJson({
UserId: 1
})
.expectStatus(200)
.stores('User', '.', { merge: true })
.stores('User', 'req.body', { merge: true });
expect(stash.getDataStore()).deep.equals({ User: { UserId: 1, name: 'Snow' } });
})

afterEach(() => {
Expand Down

0 comments on commit c05f573

Please sign in to comment.