Skip to content

Commit

Permalink
feat: setState setDataDirectory withJson file
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep committed Mar 20, 2024
1 parent 5594ebf commit d1ad8e0
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 7 deletions.
10 changes: 6 additions & 4 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}> = []
Expand Down Expand Up @@ -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', },
]
},
{
Expand Down Expand Up @@ -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', },
]
},
{
Expand Down
4 changes: 4 additions & 0 deletions docs/api/assertions/expectCookiesLike.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions docs/api/requests/setState.md
Original file line number Diff line number Diff line change
@@ -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);
```
36 changes: 35 additions & 1 deletion docs/api/requests/withJson.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Request body as json payload.

```js
withJson(payload)
withJson(file_path)
withJson(file_name)
```

## Usage
Expand All @@ -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');
Expand All @@ -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)
45 changes: 45 additions & 0 deletions docs/api/settings/setDataDirectory.md
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions docs/api/settings/setSnapshotDirectoryPath.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit d1ad8e0

Please sign in to comment.