Skip to content

Commit

Permalink
feat(http-api): improve fixtures examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte authored and Raphaël Benitte committed Jul 2, 2017
1 parent b4760cc commit f84ca4b
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ Then:
- /^(?:I )?should receive a ([1-5][0-9][0-9]) HTTP status code$/
- /^(?:I )?should receive a json response (fully )?matching$/
- /^(?:I )?should receive a collection of ([0-9]+) items? for path (.+)$/
- /^response should match snapshot (.+)$/
```

#### http API low level API
Expand Down
21 changes: 0 additions & 21 deletions examples/features/http_api/fixtures.feature

This file was deleted.

82 changes: 82 additions & 0 deletions examples/features/http_api/fixtures/fixtures.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
@http_api @fixtures
Feature: Using fixtures with http API extension

@yaml
Scenario: Setting json body from .yaml fixture file
Given I mock http call to forward request body for path /users/yaml
And set request json body from yaml_00
When I POST http://fake.io/users/yaml
Then I should receive a 200 HTTP status code
And response should match snapshot yaml_00

@yaml
Scenario: Setting form body from .yaml fixture file
Given I mock http call to forward request body for path /users/yaml
And set request form body from yaml_00
When I POST http://fake.io/users/yaml
Then I should receive a 200 HTTP status code
And response should match url encoded snapshot yaml_00

@yaml
Scenario: Setting json body from .yml fixture file
Given I mock http call to forward request body for path /users/yml
And set request json body from yaml_01
When I POST http://fake.io/users/yml
Then I should receive a 200 HTTP status code
And response should match snapshot yaml_01

@yaml
Scenario: Setting form body from .yml fixture file
Given I mock http call to forward request body for path /users/yml
And set request form body from yaml_01
When I POST http://fake.io/users/yml
Then I should receive a 200 HTTP status code
And response should match url encoded snapshot yaml_01

@text
Scenario: Setting json body from .txt fixture file
Given I mock http call to forward request body for path /users/txt
And set request json body from text_00
When I POST http://fake.io/users/txt
Then I should receive a 200 HTTP status code
And response should match snapshot text_00

@text
Scenario: Setting form body from .txt fixture file
Given I mock http call to forward request body for path /users/txt
And set request form body from text_00
When I POST http://fake.io/users/txt
Then I should receive a 200 HTTP status code
And response should match snapshot text_00

@js
Scenario: Setting json body from .js fixture file
Given I mock http call to forward request body for path /users/js
And set request json body from module_00
When I POST http://fake.io/users/js
Then I should receive a 200 HTTP status code
And response should match snapshot module_00

@js
Scenario: Setting form body from .js fixture file
Given I mock http call to forward request body for path /users/js
And set request form body from module_00
When I POST http://fake.io/users/js
Then I should receive a 200 HTTP status code
And response should match url encoded snapshot module_00

@json
Scenario: Setting json body from .json fixture file
Given I mock http call to forward request body for path /users/json
And set request json body from json_00
When I POST http://fake.io/users/json
Then I should receive a 200 HTTP status code
And response should match snapshot json_00

@json
Scenario: Setting form body from .json fixture file
Given I mock http call to forward request body for path /users/json
And set request form body from json_00
When I POST http://fake.io/users/json
Then I should receive a 200 HTTP status code
And response should match url encoded snapshot json_00
23 changes: 23 additions & 0 deletions examples/support/http_api/fixtures.steps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const querystring = require('querystring')
const { defineSupportCode } = require('cucumber')
const nock = require('nock')
const { expect } = require('chai')

defineSupportCode(({ Given, Then }) => {
Given(/^I mock http call to forward request body for path (.+)$/, function(path) {
nock('http://fake.io')
.post(path)
.reply(200, (uri, requestBody) => requestBody)
})

Then(/^response should match url encoded snapshot (.+)$/, function(snapshotId) {
const httpResponse = this.httpApiClient.getResponse()
expect(httpResponse).to.not.be.empty

return this.fixtures.load(snapshotId).then(snapshot => {
expect(httpResponse.body).to.equal(querystring.stringify(snapshot))
})
})
})
12 changes: 12 additions & 0 deletions src/extensions/http_api/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,16 @@ module.exports = ({ baseUrl = '' } = {}) => ({ Given, When, Then }) => {

expect(array.length).to.be.equal(Number(itemsNumber))
})

/**
* Verifies that response matches snapshot.
*/
Then(/^response should match snapshot (.+)$/, function(snapshotId) {
const httpResponse = this.httpApiClient.getResponse()
expect(httpResponse).to.not.be.empty

return this.fixtures.load(snapshotId).then(snapshot => {
expect(httpResponse.body).to.deep.equal(snapshot)
})
})
}
1 change: 1 addition & 0 deletions tests/__mocks__/chai.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ chainable.to = chainable
chainable.be = chainable
chainable.not = chainable
chainable.empty = chainable
chainable.deep = chainable

const expect = jest.fn(() => chainable)

Expand Down
49 changes: 35 additions & 14 deletions tests/extensions/http_api/definitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ beforeEach(() => {
require('chai').clear()
})

test('should allow to set request headers', () => {
test('set request headers', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('set request headers')
Expand All @@ -27,7 +27,7 @@ test('should allow to set request headers', () => {
expect(clientMock.httpApiClient.setHeaders).toHaveBeenCalledWith(headers)
})

test('should allow to set a single request header', () => {
test('set a single request header', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('request header to')
Expand All @@ -44,7 +44,7 @@ test('should allow to set a single request header', () => {
expect(clientMock.httpApiClient.setHeader).toHaveBeenCalledWith('Accept', 'test')
})

test('should allow to set request json body', () => {
test('set request json body', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('set request json body$')
Expand All @@ -53,7 +53,7 @@ test('should allow to set request json body', () => {
def.shouldMatch('set request json body')
})

test('should allow to set request json body from fixture file', () => {
test('set request json body from fixture file', () => {
const context = helper.define(definitions)

expect.assertions(6)
Expand All @@ -79,7 +79,7 @@ test('should allow to set request json body from fixture file', () => {
})
})

test('should allow to set request form body', () => {
test('set request form body', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('set request form body$')
Expand All @@ -88,7 +88,7 @@ test('should allow to set request form body', () => {
def.shouldMatch('set request form body')
})

test('should allow to set request form body from fixture file', () => {
test('set request form body from fixture file', () => {
const context = helper.define(definitions)

expect.assertions(6)
Expand All @@ -114,7 +114,7 @@ test('should allow to set request form body from fixture file', () => {
})
})

test('should allow to set request query', () => {
test('set request query', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('set request query')
Expand All @@ -134,7 +134,7 @@ test('should allow to set request query', () => {
expect(clientMock.httpApiClient.setQuery).toHaveBeenCalledWith(query)
})

test('should allow to pick response json property', () => {
test('pick response json property', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('pick response json')
Expand All @@ -144,7 +144,7 @@ test('should allow to pick response json property', () => {
def.shouldMatch('pick response json key as value', ['key', 'value'])
})

test('should allow to reset http client', () => {
test('reset http client', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('reset http client')
Expand All @@ -157,7 +157,7 @@ test('should allow to reset http client', () => {
expect(clientMock.httpApiClient.reset).toHaveBeenCalled()
})

test('should allow to perform a request', () => {
test('perform a request', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('GET|POST|PUT|DELETE')
Expand All @@ -173,7 +173,7 @@ test('should allow to perform a request', () => {
def.shouldMatch('DELETE /delete', ['DELETE', '/delete'])
})

test('should allow to dump response body', () => {
test('dump response body', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('dump response body')
Expand All @@ -186,7 +186,7 @@ test('should allow to dump response body', () => {
expect(clientMock.httpApiClient.getResponse).toHaveBeenCalled()
})

test('should allow to check response HTTP status code', () => {
test('check response HTTP status code', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('HTTP status code')
Expand All @@ -204,7 +204,7 @@ test('should allow to check response HTTP status code', () => {
expect(require('chai').expect).toHaveBeenCalledWith(200)
})

test('should allow to check json response', () => {
test('check json response', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('should receive a json response')
Expand All @@ -215,7 +215,7 @@ test('should allow to check json response', () => {
def.shouldMatch('should receive a json response fully matching', ['fully '])
})

test('should allow to check json collection size for a given path', () => {
test('check json collection size for a given path', () => {
const context = helper.define(definitions)

const def = context.getDefinitionByMatcher('should receive a collection of')
Expand All @@ -227,3 +227,24 @@ test('should allow to check json collection size for a given path', () => {
def.shouldMatch('should receive a collection of 1 item for path property', ['1', 'property'])
def.shouldMatch('should receive a collection of 2 items for path property', ['2', 'property'])
})

test('match snapshot', () => {
const context = helper.define(definitions)

expect.assertions(5)

const def = context.getDefinitionByMatcher('should match snapshot')
def.shouldHaveType('Then')
def.shouldNotMatch('response should match snapshot ')
def.shouldMatch('response should match snapshot snapshot', ['snapshot'])

const snapshot = { testing: true }
const worldMock = {
httpApiClient: { getResponse: jest.fn(() => ({ statusCode: 200, body: snapshot })) },
fixtures: { load: jest.fn(() => Promise.resolve(snapshot)) }
}

return def.exec(worldMock, 'snapshot').then(() => {
expect(require('chai').expect).toHaveBeenCalledWith(snapshot)
})
})

0 comments on commit f84ca4b

Please sign in to comment.