From d1ad8e0362fd5ea3d5e6a6b2ea6600c0ccb1e0a8 Mon Sep 17 00:00:00 2001 From: Anudeep Date: Wed, 20 Mar 2024 22:05:45 +0530 Subject: [PATCH] feat: setState setDataDirectory withJson file --- docs/.vitepress/config.ts | 10 ++-- docs/api/assertions/expectCookiesLike.md | 4 ++ docs/api/requests/setState.md | 52 +++++++++++++++++++ docs/api/requests/withJson.md | 36 ++++++++++++- docs/api/settings/setDataDirectory.md | 45 ++++++++++++++++ docs/api/settings/setSnapshotDirectoryPath.md | 4 +- 6 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 docs/api/requests/setState.md create mode 100644 docs/api/settings/setDataDirectory.md diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 527f6dcc..7a067085 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -3,9 +3,9 @@ const releases = 'https://github.com/pactumjs/pactum/releases'; const packages = 'https://www.npmjs.com/package/pactum'; const twitter = 'https://twitter.com/pactumjs'; +import { createWriteStream } from 'node:fs'; +import { resolve } from 'node:path'; import { SitemapStream } from 'sitemap'; -import { createWriteStream } from 'node:fs' -import { resolve } from 'node:path' import { defineConfig } from 'vitepress'; const links: Array<{url: string, lastmod?: number}> = [] @@ -84,7 +84,8 @@ const api_sidebar = [ { text: 'use', link: '/api/requests/use', }, { text: 'name', link: '/api/requests/name', }, { text: 'flow', link: '/api/requests/flow', }, - { text: 'useLogLevel', link: '/api/requests/useLogLevel', } + { text: 'useLogLevel', link: '/api/requests/useLogLevel', }, + { text: 'setState', link: '/api/requests/setState', }, ] }, { @@ -177,7 +178,8 @@ const api_sidebar = [ { text: 'setSnapshotDirectoryPath', link: '/api/settings/setSnapshotDirectoryPath', }, { text: 'setReporterAutoRun', link: '/api/settings/setReporterAutoRun', }, { text: 'setRequestDefaultRetryCount', link: '/api/settings/setRequestDefaultRetryCount', }, - { text: 'setRequestDefaultRetryDelay', link: '/api/settings/setRequestDefaultRetryDelay', } + { text: 'setRequestDefaultRetryDelay', link: '/api/settings/setRequestDefaultRetryDelay', }, + { text: 'setDataDirectory', link: '/api/settings/setDataDirectory', }, ] }, { diff --git a/docs/api/assertions/expectCookiesLike.md b/docs/api/assertions/expectCookiesLike.md index 4f748bf4..09d11668 100644 --- a/docs/api/assertions/expectCookiesLike.md +++ b/docs/api/assertions/expectCookiesLike.md @@ -10,6 +10,10 @@ Performs partial match on cookies in the response. Pass either key-value pair or > PactumJS uses [lightcookie](https://www.npmjs.com/package/lightcookie) internally to parse. +::: tip TIP +And this validation is similar to [expectJsonLike](/api/assertions/expectJsonLike). +::: + ## Syntax ```js diff --git a/docs/api/requests/setState.md b/docs/api/requests/setState.md new file mode 100644 index 00000000..5eb3fb0a --- /dev/null +++ b/docs/api/requests/setState.md @@ -0,0 +1,52 @@ +--- +tags: + - state + - set state +--- + +# setState + +**State Handlers** helps us to run specific asynchronous code that puts our application in a specific state. We can use these state handlers in our tests to reset/set state before each test case. + +## Syntax + +```js +setState(name, data?); +``` + +### ✅ Correct Usage + +```js +setState('admin user'); +setState('admin user', { data: 'some data' }); +``` + +## Arguments + +#### > name *(string)* + +Name of the state handler + +#### > data *(object)* + +Data that will be passed to the state handler + +## Examples + +### Normal + +```js +const { spec, handler } = require('pactum'); + +handler.addStateHandler('fix earth', async (ctx) => { + const { data } = ctx; + // code to add data in database + // or code to reset redis + // or custom code to centering a div +}); + +await spec + .setState('fix earth') + .get('/users') + .expectStatus(200); +``` diff --git a/docs/api/requests/withJson.md b/docs/api/requests/withJson.md index 1661f3c4..531e9325 100644 --- a/docs/api/requests/withJson.md +++ b/docs/api/requests/withJson.md @@ -13,6 +13,8 @@ Request body as json payload. ```js withJson(payload) +withJson(file_path) +withJson(file_name) ``` ## Usage @@ -35,9 +37,17 @@ await spec() Payload is a js object. +#### > file_path (string) + +relative path to the json file. + +#### > file_name (string) + +name of the file. This function looks for a file inside the `./data` folder and any folders nested within it. + ## Examples -#### JSON +#### Payload ```js const { spec } = require('pactum'); @@ -51,6 +61,30 @@ await spec() .expectStatus(201); ``` +#### File Path + +```js +const { spec } = require('pactum'); + +await spec() + .post('https://reqres.in/api/users') + .withJson('./data/user.json') + .expectStatus(201); +``` + +#### File Name + +```js +const { spec } = require('pactum'); + +await spec() + .post('https://reqres.in/api/users') + .withJson('user.json') // searches for the file inside the data folder + .expectStatus(201); +``` + + ## See Also +- [withBody](/api/requests/withBody) - [withBody](/api/requests/withBody) \ No newline at end of file diff --git a/docs/api/settings/setDataDirectory.md b/docs/api/settings/setDataDirectory.md new file mode 100644 index 00000000..fa9d4204 --- /dev/null +++ b/docs/api/settings/setDataDirectory.md @@ -0,0 +1,45 @@ +--- +tags: + - data directory path + - data folder path +--- + +# setDataDirectory + +sets data directory path. + +> Defaults to `./data` path + +## Syntax + +```js +setDataDirectory(path) +``` + +## Usage + +### ✅ Correct Usage + +```js +settings.setDataDirectory('new/path') +``` + +## Arguments + +#### > path (string) + +Data directory path. + +## Examples + +### Normal + +```js +const { settings } = require('pactum'); + +settings.setDataDirectory('new/path'); +``` + +## See Also + +- [withJson](/api/requests/withJson) \ No newline at end of file diff --git a/docs/api/settings/setSnapshotDirectoryPath.md b/docs/api/settings/setSnapshotDirectoryPath.md index 42a23aeb..f0885f4b 100644 --- a/docs/api/settings/setSnapshotDirectoryPath.md +++ b/docs/api/settings/setSnapshotDirectoryPath.md @@ -27,14 +27,14 @@ settings.setSnapshotDirectoryPath('new/path') #### > path (string) -Snapshot directory path url. +Snapshot directory path. ## Examples ### Normal ```js -const { spec, request } = require('pactum'); +const { spec, settings } = require('pactum'); settings.setSnapshotDirectoryPath('new/path');