@shopgate/pwa-core 7.30.0-alpha.6 → 7.30.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/classes/AppCommand/index.js +115 -11
- package/classes/AppCommand/spec.js +260 -6
- package/classes/AppCommandRequest/index.js +129 -20
- package/classes/AppPermissionsRequest/AppPermissionsRequest.js +45 -7
- package/classes/AppPermissionsRequest/GetAppPermissionsRequest.js +48 -9
- package/classes/AppPermissionsRequest/RequestAppPermissionsRequest.js +54 -9
- package/classes/Bridge/index.js +34 -4
- package/classes/Bridge/spec.js +24 -1
- package/classes/BrightnessRequest/index.js +59 -10
- package/classes/BrightnessRequest/spec.js +111 -6
- package/classes/BrowserConnector/index.js +180 -26
- package/classes/Conditioner/index.js +74 -8
- package/classes/Conditioner/spec.js +75 -1
- package/classes/DataRequest/index.js +116 -13
- package/classes/DevServerBridge/index.js +86 -9
- package/classes/DevServerBridge/spec.js +231 -14
- package/classes/ErrorManager/index.js +144 -20
- package/classes/ErrorManager/spec.js +244 -2
- package/classes/Event/index.js +101 -15
- package/classes/HttpRequest/index.js +182 -21
- package/classes/PipelineDependencies/index.js +42 -6
- package/classes/PipelineDependencies/spec.js +46 -3
- package/classes/PipelineManager/index.js +517 -71
- package/classes/PipelineManager/spec.js +733 -15
- package/classes/PipelineRequest/index.js +167 -19
- package/classes/PipelineRequest/mock.js +118 -21
- package/classes/PipelineRequest/spec.js +333 -2
- package/classes/PipelineSequence/index.js +34 -6
- package/classes/Request/index.js +61 -13
- package/classes/RequestBuffer/index.js +43 -6
- package/classes/RequestManager/index.js +216 -33
- package/classes/RequestManager/spec.js +188 -1
- package/classes/Scanner/index.js +246 -67
- package/classes/ScannerEvent/index.js +23 -9
- package/classes/ScannerEventHandler/index.js +39 -16
- package/classes/ScannerEventListener/index.js +84 -24
- package/classes/ScannerManager/ScanProcessingError.js +11 -3
- package/classes/ScannerManager/index.js +133 -21
- package/classes/WebStorageRequest/index.js +76 -9
- package/commands/analyticsSetCustomValues.js +8 -2
- package/commands/appPermissions.js +10 -3
- package/commands/brightness.js +33 -5
- package/commands/broadcastEvent.js +8 -2
- package/commands/cleanTab.js +11 -3
- package/commands/closeInAppBrowser.js +22 -2
- package/commands/flushTab.js +8 -2
- package/commands/getWebStorageEntry.js +11 -2
- package/commands/hideMenuBar.js +8 -2
- package/commands/hideNavigationBar.js +8 -2
- package/commands/hideSplashScreen.js +8 -2
- package/commands/onload.js +13 -3
- package/commands/openAppSettings.js +8 -2
- package/commands/openPage.js +8 -2
- package/commands/openPageExtern.js +8 -2
- package/commands/performCommandsAfterDelay.js +11 -3
- package/commands/plotProjects.js +65 -7
- package/commands/popTabToRoot.js +11 -3
- package/commands/registerEvents.js +10 -2
- package/commands/scanner.js +76 -7
- package/commands/setCookie.js +8 -2
- package/commands/setDebugLoggingEnabled.js +8 -2
- package/commands/setScrollingEnabled.js +7 -2
- package/commands/setWebStorageEntry.js +8 -2
- package/commands/shareItem.js +18 -2
- package/commands/showNavigationBar.js +8 -2
- package/commands/showTab.js +13 -2
- package/commands/unifiedTracking.js +128 -30
- package/constants/AppCommands.js +6 -1
- package/constants/AppEvents.js +9 -1
- package/constants/AppPermissions.js +57 -13
- package/constants/Command.js +1 -1
- package/constants/ErrorHandleTypes.js +2 -1
- package/constants/ErrorManager.js +15 -1
- package/constants/Pipeline.js +52 -17
- package/constants/ProcessTypes.js +3 -1
- package/constants/RequestManagerModes.js +19 -7
- package/constants/RequestTypes.js +2 -1
- package/constants/Scanner.js +39 -10
- package/constants/Trilean.js +6 -1
- package/emitters/ui.js +2 -1
- package/helpers/index.js +66 -8
- package/helpers/logGroup.js +56 -8
- package/helpers/version.js +216 -22
- package/index.js +60 -5
- package/package.json +1 -1
|
@@ -1,23 +1,61 @@
|
|
|
1
|
-
|
|
1
|
+
import "core-js/modules/es.string.replace.js";
|
|
2
|
+
import AppCommandRequest from "../AppCommandRequest";
|
|
3
|
+
|
|
4
|
+
/**
|
|
2
5
|
* The AppPermissionsRequest class is the base class for app permission related requests.
|
|
3
6
|
* It contains the logic which in necessary to establish the process of sending an
|
|
4
7
|
* app command and receiving an associated event.
|
|
5
|
-
*/
|
|
8
|
+
*/
|
|
9
|
+
class AppPermissionsRequest extends AppCommandRequest {
|
|
10
|
+
/**
|
|
6
11
|
* The constructor.
|
|
7
12
|
* @param {string} commandName The name of the command which is dispatched to the app.
|
|
8
13
|
* @param {string} eventName The event name which is called by the app to deliver the data.
|
|
9
|
-
*/
|
|
14
|
+
*/
|
|
15
|
+
constructor(commandName, eventName) {
|
|
16
|
+
super(commandName, eventName);
|
|
17
|
+
this.dispatchMock = null;
|
|
18
|
+
this.setLibVersion('18.0');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
10
22
|
* Sets a mock for the dispatch method
|
|
11
23
|
* @private
|
|
12
24
|
* @param {Function} dispatchMock The dispatch mock
|
|
13
25
|
* @returns {AppPermissionsRequest}
|
|
14
|
-
*/
|
|
26
|
+
*/
|
|
27
|
+
setDispatchMock(dispatchMock = null) {
|
|
28
|
+
this.dispatchMock = dispatchMock;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
15
33
|
* Creates title for the app command request log
|
|
16
34
|
* @returns {string}
|
|
17
|
-
*/
|
|
35
|
+
*/
|
|
36
|
+
getRequestLogTitle() {
|
|
37
|
+
const requestType = this.commandName.replace('AppPermissions', '');
|
|
38
|
+
return `AppPermissionsRequest %c${requestType}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
18
42
|
* Creates title for the app command response log
|
|
19
43
|
* @returns {string}
|
|
20
|
-
*/
|
|
44
|
+
*/
|
|
45
|
+
getResponseLogTitle() {
|
|
46
|
+
const requestType = this.commandName.replace('AppPermissions', '');
|
|
47
|
+
return `AppPermissionsResponse %c${requestType}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
21
51
|
* Dispatches the request.
|
|
22
52
|
* @return {Promise} A promise that is fulfilled when a response is received for this request.
|
|
23
|
-
*/
|
|
53
|
+
*/
|
|
54
|
+
dispatch() {
|
|
55
|
+
if (typeof this.dispatchMock === 'function') {
|
|
56
|
+
return this.dispatchMock(this.commandName, this.commandParams);
|
|
57
|
+
}
|
|
58
|
+
return super.dispatch();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export default AppPermissionsRequest;
|
|
@@ -1,19 +1,58 @@
|
|
|
1
|
-
|
|
1
|
+
import AppPermissionsRequest from "./AppPermissionsRequest";
|
|
2
|
+
import { availablePermissionsIds } from "../../constants/AppPermissions";
|
|
3
|
+
import { COMMAND_GET_APP_PERMISSIONS } from "../../constants/AppCommands";
|
|
4
|
+
|
|
5
|
+
/**
|
|
2
6
|
* The GetAppPermissionsRequest class is about to get an overview about the already granted
|
|
3
7
|
* permissions by the operation system e.g. location or camera access.
|
|
4
|
-
*/
|
|
8
|
+
*/
|
|
9
|
+
class GetAppPermissionsRequest extends AppPermissionsRequest {
|
|
10
|
+
/**
|
|
5
11
|
* The constructor.
|
|
6
|
-
*/
|
|
12
|
+
*/
|
|
13
|
+
constructor() {
|
|
14
|
+
super(COMMAND_GET_APP_PERMISSIONS);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
7
18
|
* Sets the desired permission ids for the request.
|
|
8
19
|
* @param {Array} [permissionIds=[]] The permission ids.
|
|
9
20
|
* @return {GetAppPermissionsRequest}
|
|
10
|
-
*/
|
|
21
|
+
*/
|
|
22
|
+
setPermissionIds(permissionIds = []) {
|
|
23
|
+
this.setCommandParams({
|
|
24
|
+
permissionIds
|
|
25
|
+
});
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
11
30
|
* Validates the request command parameters.
|
|
12
31
|
* @override
|
|
13
32
|
* @private
|
|
14
33
|
* @return {boolean}
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if(
|
|
19
|
-
return
|
|
34
|
+
*/
|
|
35
|
+
validateCommandParams() {
|
|
36
|
+
// Empty command params are ok for this command.
|
|
37
|
+
if (this.commandParams === null) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
const {
|
|
41
|
+
permissionIds
|
|
42
|
+
} = this.commandParams;
|
|
43
|
+
|
|
44
|
+
// If permission ids are present they have to be an array.
|
|
45
|
+
if (!Array.isArray(permissionIds)) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// An empty permissionsIds array is ok for the command.
|
|
50
|
+
if (permissionIds.length === 0) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Check if all permission ids are valid.
|
|
55
|
+
return permissionIds.every(permissionId => availablePermissionsIds.includes(permissionId));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export default GetAppPermissionsRequest;
|
|
@@ -1,19 +1,64 @@
|
|
|
1
|
-
|
|
1
|
+
import AppPermissionsRequest from "./AppPermissionsRequest";
|
|
2
|
+
import { availablePermissionsIds } from "../../constants/AppPermissions";
|
|
3
|
+
import { COMMAND_REQUEST_APP_PERMISSIONS } from "../../constants/AppCommands";
|
|
4
|
+
|
|
5
|
+
/**
|
|
2
6
|
* The RequestAppPermissionsRequest class is about to initiate the process to request
|
|
3
7
|
* additional permissions from the operating system e.g. location or camera access.
|
|
4
|
-
*/
|
|
8
|
+
*/
|
|
9
|
+
class RequestAppPermissionsRequest extends AppPermissionsRequest {
|
|
10
|
+
/**
|
|
5
11
|
* The constructor.
|
|
6
|
-
*/
|
|
12
|
+
*/
|
|
13
|
+
constructor() {
|
|
14
|
+
super(COMMAND_REQUEST_APP_PERMISSIONS);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
7
18
|
* Sets the desired permissions for the request.
|
|
8
19
|
* @param {Array} permissions The permissions.
|
|
9
20
|
* @return {RequestAppPermissionsRequest}
|
|
10
|
-
*/
|
|
21
|
+
*/
|
|
22
|
+
setPermissions(permissions) {
|
|
23
|
+
this.setCommandParams({
|
|
24
|
+
permissions
|
|
25
|
+
});
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
11
30
|
* Validates the request command parameters.
|
|
12
31
|
* @override
|
|
13
32
|
* @private
|
|
14
33
|
* @return {boolean}
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if(
|
|
19
|
-
|
|
34
|
+
*/
|
|
35
|
+
validateCommandParams() {
|
|
36
|
+
// The command can't be sent without command params.
|
|
37
|
+
if (this.commandParams === null) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
const {
|
|
41
|
+
permissions
|
|
42
|
+
} = this.commandParams;
|
|
43
|
+
|
|
44
|
+
// If permissions are present they have to be an array.
|
|
45
|
+
if (!Array.isArray(permissions) || permissions.length === 0) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return permissions.every(permission => {
|
|
49
|
+
// Check if the permission is a plain object.
|
|
50
|
+
if (permission === null || typeof permission !== 'object') {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
const {
|
|
54
|
+
permissionId
|
|
55
|
+
} = permission;
|
|
56
|
+
// Check if the permission id is valid.
|
|
57
|
+
if (!availablePermissionsIds.includes(permissionId)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export default RequestAppPermissionsRequest;
|
package/classes/Bridge/index.js
CHANGED
|
@@ -1,10 +1,40 @@
|
|
|
1
|
-
|
|
1
|
+
/* global SGJavascriptBridge */
|
|
2
|
+
import { useBrowserConnector, hasSGJavaScriptBridge } from "../../helpers";
|
|
3
|
+
import BrowserConnector from "../BrowserConnector";
|
|
4
|
+
import DevServerBridge from "../DevServerBridge";
|
|
5
|
+
|
|
6
|
+
/**
|
|
2
7
|
* Creates a new Javascript Bridge for App Commands
|
|
3
8
|
* based on the device's capabilities.
|
|
4
|
-
*/
|
|
9
|
+
*/
|
|
10
|
+
class Bridge {
|
|
11
|
+
/**
|
|
5
12
|
* Initializes the Bridge.
|
|
6
|
-
*/
|
|
13
|
+
*/
|
|
14
|
+
constructor() {
|
|
15
|
+
if (useBrowserConnector()) {
|
|
16
|
+
this.bridge = new BrowserConnector();
|
|
17
|
+
} else if (hasSGJavaScriptBridge()) {
|
|
18
|
+
this.bridge = SGJavascriptBridge;
|
|
19
|
+
} else {
|
|
20
|
+
this.bridge = new DevServerBridge();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
7
25
|
* Dispatches an app command to the native app wrapper.
|
|
8
26
|
* @param {Object} command The app command to dispatch.
|
|
9
27
|
* @param {string} libVersion The command's lib version.
|
|
10
|
-
*/
|
|
28
|
+
*/
|
|
29
|
+
dispatchCommand(command, libVersion) {
|
|
30
|
+
if ('dispatchCommandForVersion' in this.bridge) {
|
|
31
|
+
this.bridge.dispatchCommandForVersion(command, libVersion);
|
|
32
|
+
/* istanbul ignore else */
|
|
33
|
+
} else if ('dispatchCommandsForVersion' in this.bridge) {
|
|
34
|
+
this.bridge.dispatchCommandsForVersion([command], libVersion);
|
|
35
|
+
} else {
|
|
36
|
+
this.bridge.dispatchCommandsStringForVersion(JSON.stringify([command]), libVersion);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export default Bridge;
|
package/classes/Bridge/spec.js
CHANGED
|
@@ -1 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
import { useBrowserConnector, hasSGJavaScriptBridge } from "../../helpers";
|
|
2
|
+
import Bridge from "./index";
|
|
3
|
+
import BrowserConnector from "../BrowserConnector";
|
|
4
|
+
import DevServerBridge from "../DevServerBridge";
|
|
5
|
+
jest.mock("../../helpers", () => ({
|
|
6
|
+
...jest.requireActual("../../helpers"),
|
|
7
|
+
useBrowserConnector: jest.fn(),
|
|
8
|
+
hasSGJavaScriptBridge: jest.fn()
|
|
9
|
+
}));
|
|
10
|
+
describe('Bridge', () => {
|
|
11
|
+
let bridge;
|
|
12
|
+
it('should use the BrowserConnector', () => {
|
|
13
|
+
useBrowserConnector.mockReturnValueOnce(true);
|
|
14
|
+
hasSGJavaScriptBridge.mockReturnValueOnce(false);
|
|
15
|
+
bridge = new Bridge();
|
|
16
|
+
expect(bridge.bridge instanceof BrowserConnector).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
it('should use the DevServerBridge', () => {
|
|
19
|
+
useBrowserConnector.mockReturnValueOnce(false);
|
|
20
|
+
hasSGJavaScriptBridge.mockReturnValueOnce(false);
|
|
21
|
+
bridge = new Bridge();
|
|
22
|
+
expect(bridge.bridge instanceof DevServerBridge).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -1,17 +1,66 @@
|
|
|
1
|
-
import
|
|
1
|
+
import event from "../Event";
|
|
2
|
+
import AppCommand from "../AppCommand";
|
|
3
|
+
import { logger } from "../../helpers";
|
|
4
|
+
const LIB_VERSION = '17.0';
|
|
5
|
+
const GET_COMMAND_NAME = 'getCurrentBrightness';
|
|
6
|
+
const RESPONSE_EVENT_NAME = 'currentBrightnessResponse';
|
|
7
|
+
|
|
8
|
+
/**
|
|
2
9
|
* Brightness request handler.
|
|
3
10
|
*
|
|
4
11
|
* Maintains the request command and waits for a response to resolve a promise returned by
|
|
5
12
|
* `.dispatch`.
|
|
6
|
-
*/
|
|
13
|
+
*/
|
|
14
|
+
export class BrightnessRequest {
|
|
15
|
+
/**
|
|
7
16
|
* Constructor.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
17
|
+
*/
|
|
18
|
+
constructor() {
|
|
19
|
+
/**
|
|
20
|
+
* Handles responses
|
|
21
|
+
* @param {Object} response Response.
|
|
22
|
+
*/
|
|
23
|
+
this.handleResponse = response => {
|
|
24
|
+
const queueEntry = this.responseQueue.shift();
|
|
25
|
+
if (typeof queueEntry === 'undefined') {
|
|
26
|
+
logger.error('currentBrightnessResponse received but the response handler queue is empty.');
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const {
|
|
30
|
+
resolve,
|
|
31
|
+
reject
|
|
32
|
+
} = queueEntry;
|
|
33
|
+
if (!response.hasOwnProperty('brightness')) {
|
|
34
|
+
reject(new Error('Invalid response for currentBrightnessResponse. Missing brightness property.'));
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
resolve(response.brightness);
|
|
38
|
+
};
|
|
39
|
+
this.responseQueue = [];
|
|
40
|
+
event.addCallback(RESPONSE_EVENT_NAME, this.handleResponse);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
12
43
|
* Dispatches the request.
|
|
13
44
|
* @returns {Promise}
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
45
|
+
*/
|
|
46
|
+
dispatch() {
|
|
47
|
+
return new Promise(async (resolve, reject) => {
|
|
48
|
+
this.responseQueue.push({
|
|
49
|
+
resolve,
|
|
50
|
+
reject
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Prepare the AppComand.
|
|
54
|
+
const command = new AppCommand().setCommandName(GET_COMMAND_NAME).setLibVersion(LIB_VERSION);
|
|
55
|
+
|
|
56
|
+
// Dispatch the command. The method will resolve with FALSE in case of an error.
|
|
57
|
+
const result = await command.dispatch();
|
|
58
|
+
if (result === false) {
|
|
59
|
+
// Remove the queue entry when the dispatch failed.
|
|
60
|
+
this.responseQueue.pop();
|
|
61
|
+
reject(new Error('getCurrentBrightness command dispatch failed'));
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export default new BrightnessRequest();
|
|
@@ -1,6 +1,111 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
/* eslint-disable import/named */
|
|
2
|
+
import { mockedSetCommandName, mockedSetLibVersion, mockedDispatch, triggerDispatchError } from "../AppCommand";
|
|
3
|
+
/* eslint-enable import/named */
|
|
4
|
+
|
|
5
|
+
const mockedAddCallback = jest.fn();
|
|
6
|
+
jest.mock("../Event", () => ({
|
|
7
|
+
addCallback: (...args) => {
|
|
8
|
+
mockedAddCallback(...args);
|
|
9
|
+
}
|
|
10
|
+
}));
|
|
11
|
+
jest.mock("../AppCommand");
|
|
12
|
+
const mockedLoggerError = jest.fn();
|
|
13
|
+
jest.mock("../../helpers", () => ({
|
|
14
|
+
logger: {
|
|
15
|
+
error: (...args) => {
|
|
16
|
+
mockedLoggerError(...args);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}));
|
|
20
|
+
describe('BrightnessRequest', () => {
|
|
21
|
+
/* eslint-disable global-require */
|
|
22
|
+
const brightnessRequest = require("./index").default;
|
|
23
|
+
const {
|
|
24
|
+
BrightnessRequest
|
|
25
|
+
} = require("./index");
|
|
26
|
+
/* eslint-enable global-require */
|
|
27
|
+
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
mockedLoggerError.mockClear();
|
|
30
|
+
mockedAddCallback.mockClear();
|
|
31
|
+
mockedSetCommandName.mockClear();
|
|
32
|
+
mockedSetLibVersion.mockClear();
|
|
33
|
+
mockedDispatch.mockClear();
|
|
34
|
+
|
|
35
|
+
// Reset the queue for each test
|
|
36
|
+
brightnessRequest.responseQueue = [];
|
|
37
|
+
});
|
|
38
|
+
it('should export an already instantiated class', () => {
|
|
39
|
+
expect(brightnessRequest).toBeInstanceOf(BrightnessRequest);
|
|
40
|
+
expect(brightnessRequest.responseQueue).toEqual([]);
|
|
41
|
+
});
|
|
42
|
+
it('should dispatch a command and prepare a handler for response', async () => {
|
|
43
|
+
brightnessRequest.dispatch().then(result => {
|
|
44
|
+
expect(mockedSetCommandName).toHaveBeenCalled();
|
|
45
|
+
expect(mockedSetCommandName).toHaveBeenLastCalledWith('getCurrentBrightness');
|
|
46
|
+
expect(mockedSetLibVersion).toHaveBeenCalledTimes(1);
|
|
47
|
+
expect(mockedSetLibVersion).toHaveBeenLastCalledWith('17.0');
|
|
48
|
+
expect(mockedDispatch).toHaveBeenCalledTimes(1);
|
|
49
|
+
expect(brightnessRequest.responseQueue).toHaveLength(0);
|
|
50
|
+
expect(result).toBe(100);
|
|
51
|
+
});
|
|
52
|
+
await brightnessRequest.handleResponse({
|
|
53
|
+
brightness: 100
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
it('should handle two parallel requests as expected', async () => {
|
|
57
|
+
// Prepare two requests
|
|
58
|
+
const requestOne = brightnessRequest.dispatch();
|
|
59
|
+
const requestTwo = brightnessRequest.dispatch();
|
|
60
|
+
|
|
61
|
+
// Check if the queue looks like expected
|
|
62
|
+
expect(brightnessRequest.responseQueue).toHaveLength(2);
|
|
63
|
+
requestOne.then(result => {
|
|
64
|
+
expect(brightnessRequest.responseQueue).toHaveLength(1);
|
|
65
|
+
expect(result).toBe(100);
|
|
66
|
+
});
|
|
67
|
+
requestTwo.then(result => {
|
|
68
|
+
expect(brightnessRequest.responseQueue).toHaveLength(0);
|
|
69
|
+
expect(result).toBe(80);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Simulate incoming of the first event
|
|
73
|
+
await brightnessRequest.handleResponse({
|
|
74
|
+
brightness: 100
|
|
75
|
+
});
|
|
76
|
+
await brightnessRequest.handleResponse({
|
|
77
|
+
brightness: 80
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
it('should reject a promise when response is not valid', done => {
|
|
81
|
+
const assertError = new Error('Did not reject');
|
|
82
|
+
brightnessRequest.dispatch().then(() => {
|
|
83
|
+
throw assertError;
|
|
84
|
+
}).catch(e => {
|
|
85
|
+
expect(e).not.toBe(assertError);
|
|
86
|
+
done();
|
|
87
|
+
});
|
|
88
|
+
brightnessRequest.handleResponse({
|
|
89
|
+
brightnessFFOFOF: 100
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
it('should reject a promise when the command dispatch failed', async () => {
|
|
93
|
+
// Configure the AppCommand mock to respolve with an error.
|
|
94
|
+
triggerDispatchError();
|
|
95
|
+
try {
|
|
96
|
+
await brightnessRequest.dispatch();
|
|
97
|
+
} catch (e) {
|
|
98
|
+
expect(e).toBeInstanceOf(Error);
|
|
99
|
+
expect(e.message).toContain('getCurrentBrightness');
|
|
100
|
+
expect(brightnessRequest.responseQueue).toHaveLength(0);
|
|
101
|
+
expect(mockedDispatch).toHaveBeenCalledTimes(1);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
it('should handle incoming events which did not have a dispatch', () => {
|
|
105
|
+
const result = brightnessRequest.handleResponse({
|
|
106
|
+
brightness: 100
|
|
107
|
+
});
|
|
108
|
+
expect(result).toBe(undefined);
|
|
109
|
+
expect(mockedLoggerError).toHaveBeenCalledTimes(1);
|
|
110
|
+
});
|
|
111
|
+
});
|