Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(scenarios): added scenario examples #145

Merged
merged 1 commit into from
Dec 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 55 additions & 29 deletions SCENARIOS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,75 @@

Parrot scenarios describe combinations of data that you want to develop against. A scenario is comprised of multiple mocks that each describe how to match against an incoming request and what response to return.

## Example
## Examples

```js
import graphql from 'parrot-graphql';
import casual from 'casual'; // for generating fake data
import schema from './schema'; // our GraphQL schema
Below are some examples of how to configure your Parrot scenarios using different methods.
### Response as an object

```js
const scenarios = {
'has a ship log': [
'has a ship log': [
{
request: '/ship_log',
request: '/ship_log', // If request is a string, the HTTP method defaults to GET
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually have an open issue for this #101

response: {
body: require('./mocks/shipLog.json'),
delay: 1200,
},
},
{
request: {
path: '/ship_log',
method: 'POST',
delay: 1200, // Optional, defaults to 0
status: 200 // Optional, defaults to 200
},
response: req => req.body,
},
],
'has a random ship log': [
{
request: '/ship_log',
response: () => [
{
port: casual.city,
captain: casual.full_name,
},
],
},
],
'has a server error': [
}
```

### Response as a function

```js
const scenarios = {
'has a ship log': [
{
request: '/ship_log',
response: {
status: 500,
// Whatever is returned by this function will be put in the body of the response
// Status will always be 200 for functions
response: () => {
return require('./mocks/shipLog.json');
},
},
],
}
```

## Functional Scenarios

```js
const scenarios = {
'has one ship': [
// Instead of an object with request/response, the function will
// handle all requests. Use the `match` function to match routes.
// Use this to set dynamic HTTP status codes.
(req, match) => {
if (match({ path: '/ship_log' })) {
return {
status: 200,
body: require('./mocks/shipLog.json'),
delay: 100,
}
}
return {
status: 404,
body: { error: 'Resource Not Found' },
delay: 100
}
}]
}
```

### GraphQL Example

```js
import graphql from 'parrot-graphql';
import schema from './schema'; // our GraphQL schema

const scenarios = {
'has a ship log from GraphQL': [
graphql('/graphql', schema, () => ({
ShipLog: () => require('./mocks/shipLog.json'),
Expand Down