darvin-testing-framework
Advanced tools
Comparing version 1.6.0 to 1.7.0
74
index.js
@@ -95,3 +95,6 @@ 'use strict'; | ||
return { id, name }; | ||
return { | ||
id, | ||
name | ||
}; | ||
} | ||
@@ -143,7 +146,74 @@ | ||
expect(step.bot.map(JSON.stringify)).toContain(JSON.stringify(response)); | ||
const isValidResponse = step.bot.some(expectedResponses => { | ||
if (expectedResponses.length !== response.length) { | ||
return false; | ||
} | ||
return expectedResponses.every((expectedResponse, i) => | ||
this._verify(expectedResponse, response[i], this._verifyResponse.bind(this))); | ||
}); | ||
if (!isValidResponse) { | ||
throw Error(`Unexpected response. Expected: ${JSON.stringify(step.bot)}. Actual: ${JSON.stringify(response)}`); | ||
} | ||
} | ||
} | ||
_verify(expected, actual, validation) { | ||
let keys = Object.keys(expected); | ||
if (Object.keys(actual).length !== keys.length) { | ||
return false; | ||
} | ||
for (const key of keys) { | ||
let isValid = validation(expected, actual, key); | ||
if (!isValid) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
_verifyResponse(expectedResponse, actualResponse, key) { | ||
switch (key) { | ||
case 'text': | ||
const expectedText = expectedResponse.text; | ||
if (Array.isArray(expectedText)) { | ||
return expectedText.includes(actualResponse.text); | ||
} else { | ||
return expectedText === actualResponse.text; | ||
} | ||
case 'template': | ||
return this._verify(expectedResponse.template, actualResponse.template, this._verifyTemplate.bind(this)); | ||
default: | ||
return JSON.stringify(expectedResponse[key]) === JSON.stringify(actualResponse[key]); | ||
}; | ||
} | ||
_verifyTemplate(expectedTemplate, actualTemplate, key) { | ||
switch (key) { | ||
case 'buttons': | ||
if (expectedTemplate.buttons.length !== actualTemplate.buttons.length) { | ||
return false; | ||
} | ||
return expectedTemplate.buttons.every((expectedButton, i) => | ||
this._verify(expectedButton, actualTemplate.buttons[i], this._verifyTemplateButton.bind(this))); | ||
default: | ||
return JSON.stringify(expectedTemplate[key]) === JSON.stringify(actualTemplate[key]); | ||
}; | ||
} | ||
_verifyTemplateButton(expectedButton, actualButton, key) { | ||
switch (key) { | ||
case 'url': | ||
return actualButton.url.startsWith(expectedButton.url); | ||
default: | ||
return JSON.stringify(expectedButton[key]) === JSON.stringify(actualButton[key]); | ||
}; | ||
} | ||
} | ||
module.exports = TestingFramework; |
{ | ||
"name": "darvin-testing-framework", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "A framework for testing Darvin.ai bots.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -106,4 +106,20 @@ # Darvin Testing Framework | ||
- `bot` - what is the expected answer from the bot as an array of optional behavior. Each element is an array of messages. The step is considered valid if any sequence of messages matches the actual chatbot response | ||
- `text` - text of a single message | ||
- `text` - text of a single message or an array of possible messages | ||
- `quickReplies` - array of quick reply options | ||
- `template` - an object describing complex response elements, such as buttons | ||
Example: | ||
```json | ||
"template": { | ||
"type": "button", | ||
"buttons": [{ | ||
"url": "https://webviews.darvin.ai/v1/bots/BOT_ID/...", | ||
"title": "Pick country" | ||
}] | ||
} | ||
``` | ||
_Note: the 'url' is checked for starts-with instead of equality_ | ||
- `mocks` - a dictionary that allows you to mock the responses of specific URLs and HTTP actions against those URLs | ||
@@ -110,0 +126,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17277
197
197