Skip to content

Commit

Permalink
feat(plugin): add plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdasberg committed Jul 16, 2018
1 parent 18a09f5 commit c5187f0
Show file tree
Hide file tree
Showing 14 changed files with 481 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/features/actions.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@actions
Feature: Actions

Developers must be able to:

- reset the mock state to defaults
- set the mock state to passthroughs

in order to run the application against mocks.

Background:
Given the following mock state
| name | scenario |
| get items | crypto-currencies |
| post item | ok |

# Verify after resetting the mock state to default

Scenario: Reset mock state to defaults
Given I open the test page
When I select scenario crypto-exchanges for mock get items
And I set delay to 2000 for mock get items
And I select scenario nok for mock post item
And I set delay to 2000 for mock post item
And I reset the mocks to default
And I get the items
Then the crypto-currencies response is returned for get items
And the ok response is returned for post item

# Verify after resetting the scenario's to passThrough

Scenario: Set mocks to passThroughs
Given I open the test page
And I set the mocks to passThroughs
And I get the items
Then the passThrough response is returned for get items
When I enter Ripple and post the item
Then the passThrough response is returned for post item
20 changes: 20 additions & 0 deletions test/features/binary.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@binary
Feature: Binary data

Developers must be able to:

- download files

Background:
Given the following mock state
| name | scenario |
| get items | crypto-currencies |
| post item | ok |

# Verify after resetting the mock state to default

Scenario: Reset mock state to defaults
Given I open the test page
When I select scenario binary for mock get items
And I download the binary file
Then the binary response is downloaded
25 changes: 25 additions & 0 deletions test/features/default.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@default
Feature: Default responses

- When an api call matches the mock request, the default response should be returned.

Background:
Given the following mock state
| name | scenario |
| get items | crypto-currencies |
| post item | ok |

Scenario: Get the items
Given I open the test page
And I get the items
Then the crypto-currencies response is returned for get items

Scenario: Get the items as jsonp
Given I open the test page
And I get the items as jsonp
Then the crypto-currencies response is returned for get items

Scenario: Post the item
Given I open the test page
When I enter Ripple and post the item
Then the ok response is returned for post item
35 changes: 35 additions & 0 deletions test/features/update-mock-state.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@update-mock-state
Feature: Update mock state

Developers must be able to:

- Update mock state
- select scenario
- echo request
- delay response

in order to run the application against mocks.

Background:
Given the following mock state
| name | scenario |
| get items | crypto-currencies |
| post item | ok |

# Verify after selecting a scenario

Scenario: Update the mock state scenario and get the items
Given I open the test page
When I select scenario crypto-exchanges for mock get items
And I get the items
Then the crypto-exchanges response is returned for get items

# Verify after delaying the response

Scenario: Update the mock state delay and get the items
Given I open the test page
When I set delay to 2000 for mock get items
And I get the items
Then the items are not yet fetched
When I wait a 2000 milliseconds
Then the items are fetched
39 changes: 39 additions & 0 deletions test/features/update-variables-state.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@update-variables-state
Feature: Update variables state

Developers must be able to:

- Update variable state
- add a variable
- update a variable
- delete a variable

in order to run the application against mocks.

Background:
Given the following mock state
| name | scenario |
| get items | crypto-currencies |
| post item | ok |

# Verify after selecting a scenario

Scenario: Add a variable and get the items (interpolated)
Given I open the test page
When I add variable coinName with value Cool
And I get the items
Then the response is interpolated with variable Cool

Scenario: Update a variable and get the items (interpolated)
Given I open the test page
When I add variable coinName with value Cool
And I update variable coinName with value Super
And I get the items
Then the response is interpolated with variable Super

Scenario: Delete a variable and get the items (interpolated)
Given I open the test page
When I add variable coinName with value Cool
And I delete variable coinName
And I get the items
Then the crypto-currencies response is returned for get items
49 changes: 49 additions & 0 deletions test/mocks/get-items.mock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "get items",
"isArray": true,
"request": {
"url": "/items",
"method": "GET"
},
"responses": {
"crypto-currencies": {
"status": 200,
"data": [
"Bitcoin",
"Bitcoin Cash",
"Etherium",
"Litecoin",
"Some%%coinName%%Coin"
],
"headers": {
"Content-type": "application/json",
"custom": "custom"
},
"statusText": "text",
"default": true
},
"crypto-exchanges": {
"status": 200,
"data": [
"Coinbase",
"Binance",
"Hitbtc"
],
"headers": {
"Content-type": "application/json",
"custom": "custom"
}
},
"binary": {
"file": "mocks/test.pdf",
"headers": {
"Content-type": "application/pdf",
"filename": "test.pdf"
}
},
"server-error": {
"default": false,
"status": 500
}
}
}
24 changes: 24 additions & 0 deletions test/mocks/post-item.mock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "post item",
"isArray": true,
"request": {
"url": "/items",
"method": "POST",
"payload": {
"item": "^[a-zA-Z]{3,10}$"
}
},
"responses": {
"ok": {
"status": 200,
"default": true
},
"nok": {
"status": 409
},
"server-error": {
"default": false,
"status": 500
}
}
}
Binary file added test/mocks/test.pdf
Binary file not shown.
28 changes: 28 additions & 0 deletions test/pos/page.po.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class PagePO {
get data() { return browser.element('.data'); }
get status() { return browser.element('.status'); }
get done() { return browser.element('.done'); }
get input() { return browser.element('#item'); }

constructor() {
this.buttons = new PageButtons();
}

async open() {
const url = '/index.html';
await browser.url(url);
await browser.waitUntil(async () => {
const header = await browser.getText('h1');
return header.indexOf('ng-apimock test example app') > -1;
}, 5000);
}
}

class PageButtons {
get get() { return browser.element('button*=get'); }
get binary() { return browser.element('button*=binary'); }
get getAsJsonp() { return browser.element('button*=get as jsonp'); }
get post() { return browser.element('button*=post'); }
}

module.exports = PagePO;
21 changes: 21 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const apimock = require('@ng-apimock/core');
const connect = require('connect');
const path = require('path');
const serveStatic = require('serve-static');
const app = connect();

apimock.processor.process({src: path.join(__dirname, 'mocks')});
app.use(apimock.middleware);
app.use('/', serveStatic(path.join(require.resolve('@ng-apimock/test-application'), '..')));
app.use('/items', function (request, response, next) {
response.writeHead(200, {'Content-Type': 'application/json'});
if (request.method === 'GET') {
response.end('["passThrough"]');
} else if (request.method === 'POST') {
response.end('["passThrough"]');
} else {
next();
}
});
app.listen(9900);
console.log('ng-apimock-angular-test-app is running on port 9900');
54 changes: 54 additions & 0 deletions test/step_definitions/client.steps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(() => {
const {Given, When, After} = require('cucumber');

Given(/^the following mock state$/, checkMockState);

When(/^I add variable (.*) with value (.*)/, addVariable);
When(/^I delete variable (.*)/, deleteVariable);
When(/^I reset the mocks to default$/, resetMocksToDefault);
When(/^I select scenario (.*) for mock (.*)$/, selectScenario);
When(/^I set delay to (\d+) for mock (.*)$/, delayResponse);
When(/^I set the mocks to passThroughs$/, setMocksToPassThrough);
When(/^I update variable (.*) with value (.*)/, updateVariable);
When(/^I wait a (\d+) milliseconds$/, waitSeconds);

After(async () => await ngApimock.resetMocksToDefault());

async function addVariable(key, value) {
await ngApimock.setVariable(key, value);
}

async function checkMockState(dataTable) {
const mocks = await ngApimock.getMocks();
dataTable.rows()
.forEach((row) => expect(mocks.state[row[0]].scenario).to.equal(row[1]));
}

async function delayResponse(delay, name) {
await ngApimock.delayResponse(name, parseInt(delay));
}

async function deleteVariable(key) {
await ngApimock.deleteVariable(key);
}

async function selectScenario(scenario, name) {
await ngApimock.selectScenario(name, scenario);
}

async function resetMocksToDefault() {
await ngApimock.resetMocksToDefault();
}

async function setMocksToPassThrough() {
await ngApimock.setMocksToPassThrough();
}

async function updateVariable(key, value) {
await ngApimock.setVariable(key, value);
}

async function waitSeconds(wait) {
await browser.pause(wait);
}
})();
Loading

0 comments on commit c5187f0

Please sign in to comment.