Skip to content

Commit c5187f0

Browse files
committed
feat(plugin): add plugin tests
1 parent 18a09f5 commit c5187f0

14 files changed

+481
-0
lines changed

test/features/actions.feature

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@actions
2+
Feature: Actions
3+
4+
Developers must be able to:
5+
6+
- reset the mock state to defaults
7+
- set the mock state to passthroughs
8+
9+
in order to run the application against mocks.
10+
11+
Background:
12+
Given the following mock state
13+
| name | scenario |
14+
| get items | crypto-currencies |
15+
| post item | ok |
16+
17+
# Verify after resetting the mock state to default
18+
19+
Scenario: Reset mock state to defaults
20+
Given I open the test page
21+
When I select scenario crypto-exchanges for mock get items
22+
And I set delay to 2000 for mock get items
23+
And I select scenario nok for mock post item
24+
And I set delay to 2000 for mock post item
25+
And I reset the mocks to default
26+
And I get the items
27+
Then the crypto-currencies response is returned for get items
28+
And the ok response is returned for post item
29+
30+
# Verify after resetting the scenario's to passThrough
31+
32+
Scenario: Set mocks to passThroughs
33+
Given I open the test page
34+
And I set the mocks to passThroughs
35+
And I get the items
36+
Then the passThrough response is returned for get items
37+
When I enter Ripple and post the item
38+
Then the passThrough response is returned for post item

test/features/binary.feature

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@binary
2+
Feature: Binary data
3+
4+
Developers must be able to:
5+
6+
- download files
7+
8+
Background:
9+
Given the following mock state
10+
| name | scenario |
11+
| get items | crypto-currencies |
12+
| post item | ok |
13+
14+
# Verify after resetting the mock state to default
15+
16+
Scenario: Reset mock state to defaults
17+
Given I open the test page
18+
When I select scenario binary for mock get items
19+
And I download the binary file
20+
Then the binary response is downloaded

test/features/default.feature

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@default
2+
Feature: Default responses
3+
4+
- When an api call matches the mock request, the default response should be returned.
5+
6+
Background:
7+
Given the following mock state
8+
| name | scenario |
9+
| get items | crypto-currencies |
10+
| post item | ok |
11+
12+
Scenario: Get the items
13+
Given I open the test page
14+
And I get the items
15+
Then the crypto-currencies response is returned for get items
16+
17+
Scenario: Get the items as jsonp
18+
Given I open the test page
19+
And I get the items as jsonp
20+
Then the crypto-currencies response is returned for get items
21+
22+
Scenario: Post the item
23+
Given I open the test page
24+
When I enter Ripple and post the item
25+
Then the ok response is returned for post item
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@update-mock-state
2+
Feature: Update mock state
3+
4+
Developers must be able to:
5+
6+
- Update mock state
7+
- select scenario
8+
- echo request
9+
- delay response
10+
11+
in order to run the application against mocks.
12+
13+
Background:
14+
Given the following mock state
15+
| name | scenario |
16+
| get items | crypto-currencies |
17+
| post item | ok |
18+
19+
# Verify after selecting a scenario
20+
21+
Scenario: Update the mock state scenario and get the items
22+
Given I open the test page
23+
When I select scenario crypto-exchanges for mock get items
24+
And I get the items
25+
Then the crypto-exchanges response is returned for get items
26+
27+
# Verify after delaying the response
28+
29+
Scenario: Update the mock state delay and get the items
30+
Given I open the test page
31+
When I set delay to 2000 for mock get items
32+
And I get the items
33+
Then the items are not yet fetched
34+
When I wait a 2000 milliseconds
35+
Then the items are fetched
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@update-variables-state
2+
Feature: Update variables state
3+
4+
Developers must be able to:
5+
6+
- Update variable state
7+
- add a variable
8+
- update a variable
9+
- delete a variable
10+
11+
in order to run the application against mocks.
12+
13+
Background:
14+
Given the following mock state
15+
| name | scenario |
16+
| get items | crypto-currencies |
17+
| post item | ok |
18+
19+
# Verify after selecting a scenario
20+
21+
Scenario: Add a variable and get the items (interpolated)
22+
Given I open the test page
23+
When I add variable coinName with value Cool
24+
And I get the items
25+
Then the response is interpolated with variable Cool
26+
27+
Scenario: Update a variable and get the items (interpolated)
28+
Given I open the test page
29+
When I add variable coinName with value Cool
30+
And I update variable coinName with value Super
31+
And I get the items
32+
Then the response is interpolated with variable Super
33+
34+
Scenario: Delete a variable and get the items (interpolated)
35+
Given I open the test page
36+
When I add variable coinName with value Cool
37+
And I delete variable coinName
38+
And I get the items
39+
Then the crypto-currencies response is returned for get items

test/mocks/get-items.mock.json

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "get items",
3+
"isArray": true,
4+
"request": {
5+
"url": "/items",
6+
"method": "GET"
7+
},
8+
"responses": {
9+
"crypto-currencies": {
10+
"status": 200,
11+
"data": [
12+
"Bitcoin",
13+
"Bitcoin Cash",
14+
"Etherium",
15+
"Litecoin",
16+
"Some%%coinName%%Coin"
17+
],
18+
"headers": {
19+
"Content-type": "application/json",
20+
"custom": "custom"
21+
},
22+
"statusText": "text",
23+
"default": true
24+
},
25+
"crypto-exchanges": {
26+
"status": 200,
27+
"data": [
28+
"Coinbase",
29+
"Binance",
30+
"Hitbtc"
31+
],
32+
"headers": {
33+
"Content-type": "application/json",
34+
"custom": "custom"
35+
}
36+
},
37+
"binary": {
38+
"file": "mocks/test.pdf",
39+
"headers": {
40+
"Content-type": "application/pdf",
41+
"filename": "test.pdf"
42+
}
43+
},
44+
"server-error": {
45+
"default": false,
46+
"status": 500
47+
}
48+
}
49+
}

test/mocks/post-item.mock.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "post item",
3+
"isArray": true,
4+
"request": {
5+
"url": "/items",
6+
"method": "POST",
7+
"payload": {
8+
"item": "^[a-zA-Z]{3,10}$"
9+
}
10+
},
11+
"responses": {
12+
"ok": {
13+
"status": 200,
14+
"default": true
15+
},
16+
"nok": {
17+
"status": 409
18+
},
19+
"server-error": {
20+
"default": false,
21+
"status": 500
22+
}
23+
}
24+
}

test/mocks/test.pdf

190 KB
Binary file not shown.

test/pos/page.po.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class PagePO {
2+
get data() { return browser.element('.data'); }
3+
get status() { return browser.element('.status'); }
4+
get done() { return browser.element('.done'); }
5+
get input() { return browser.element('#item'); }
6+
7+
constructor() {
8+
this.buttons = new PageButtons();
9+
}
10+
11+
async open() {
12+
const url = '/index.html';
13+
await browser.url(url);
14+
await browser.waitUntil(async () => {
15+
const header = await browser.getText('h1');
16+
return header.indexOf('ng-apimock test example app') > -1;
17+
}, 5000);
18+
}
19+
}
20+
21+
class PageButtons {
22+
get get() { return browser.element('button*=get'); }
23+
get binary() { return browser.element('button*=binary'); }
24+
get getAsJsonp() { return browser.element('button*=get as jsonp'); }
25+
get post() { return browser.element('button*=post'); }
26+
}
27+
28+
module.exports = PagePO;

test/server.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const apimock = require('@ng-apimock/core');
2+
const connect = require('connect');
3+
const path = require('path');
4+
const serveStatic = require('serve-static');
5+
const app = connect();
6+
7+
apimock.processor.process({src: path.join(__dirname, 'mocks')});
8+
app.use(apimock.middleware);
9+
app.use('/', serveStatic(path.join(require.resolve('@ng-apimock/test-application'), '..')));
10+
app.use('/items', function (request, response, next) {
11+
response.writeHead(200, {'Content-Type': 'application/json'});
12+
if (request.method === 'GET') {
13+
response.end('["passThrough"]');
14+
} else if (request.method === 'POST') {
15+
response.end('["passThrough"]');
16+
} else {
17+
next();
18+
}
19+
});
20+
app.listen(9900);
21+
console.log('ng-apimock-angular-test-app is running on port 9900');

test/step_definitions/client.steps.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
(() => {
2+
const {Given, When, After} = require('cucumber');
3+
4+
Given(/^the following mock state$/, checkMockState);
5+
6+
When(/^I add variable (.*) with value (.*)/, addVariable);
7+
When(/^I delete variable (.*)/, deleteVariable);
8+
When(/^I reset the mocks to default$/, resetMocksToDefault);
9+
When(/^I select scenario (.*) for mock (.*)$/, selectScenario);
10+
When(/^I set delay to (\d+) for mock (.*)$/, delayResponse);
11+
When(/^I set the mocks to passThroughs$/, setMocksToPassThrough);
12+
When(/^I update variable (.*) with value (.*)/, updateVariable);
13+
When(/^I wait a (\d+) milliseconds$/, waitSeconds);
14+
15+
After(async () => await ngApimock.resetMocksToDefault());
16+
17+
async function addVariable(key, value) {
18+
await ngApimock.setVariable(key, value);
19+
}
20+
21+
async function checkMockState(dataTable) {
22+
const mocks = await ngApimock.getMocks();
23+
dataTable.rows()
24+
.forEach((row) => expect(mocks.state[row[0]].scenario).to.equal(row[1]));
25+
}
26+
27+
async function delayResponse(delay, name) {
28+
await ngApimock.delayResponse(name, parseInt(delay));
29+
}
30+
31+
async function deleteVariable(key) {
32+
await ngApimock.deleteVariable(key);
33+
}
34+
35+
async function selectScenario(scenario, name) {
36+
await ngApimock.selectScenario(name, scenario);
37+
}
38+
39+
async function resetMocksToDefault() {
40+
await ngApimock.resetMocksToDefault();
41+
}
42+
43+
async function setMocksToPassThrough() {
44+
await ngApimock.setMocksToPassThrough();
45+
}
46+
47+
async function updateVariable(key, value) {
48+
await ngApimock.setVariable(key, value);
49+
}
50+
51+
async function waitSeconds(wait) {
52+
await browser.pause(wait);
53+
}
54+
})();

0 commit comments

Comments
 (0)