screenshot.test.js
5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2016 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*/
/* globals Promise, sinon, describe, it, beforeEach, afterEach, expect */
/* eslint max-nested-callbacks: 0 */
define([
'screens/player/shared/serviceadmin',
'screens/player/shared/test-helper/stub-service-interface',
'screens/player/shared/test-helper/service',
'screens/player/shared/test-helper/command-handler',
'screens/player/store/store',
'screens/player/firmware/preferences/preferences',
'screens/player/firmware/screenshot/screenshot',
'screens/player/firmware/network/network',
'screens/player/firmware/log/logger',
'screens/player/command/command',
'screens/player/command/screenshot'
], function(ServiceAdmin, stubServiceInterface, serviceTestHelper, commandHandlerTestHelper, Store, Preferences, Screenshot, Network, Logger, PlayerCommand, screenshotCmd) {
'use strict';
describe('screens/player/command/screenshot', function() {
var stubStoreService, stubPreferencesService, stubScreenshotService, stubNetworkService, stubLoggerService;
var deviceId;
beforeEach(function() {
stubStoreService = stubServiceInterface(ServiceAdmin, Store);
stubPreferencesService = stubServiceInterface(ServiceAdmin, Preferences);
stubScreenshotService = stubServiceInterface(ServiceAdmin, Screenshot);
stubNetworkService = stubServiceInterface(ServiceAdmin, Network);
stubLoggerService = stubServiceInterface(ServiceAdmin, Logger);
stubLoggerService.api.getWriter.returns({
log: sinon.stub(),
error: sinon.stub()
});
deviceId = 'foo';
stubPreferencesService.api.getPreferences.returns({ device: deviceId });
});
afterEach(function() {
stubStoreService.restore();
stubPreferencesService.restore();
stubScreenshotService.restore();
stubNetworkService.restore();
stubLoggerService.restore();
});
serviceTestHelper.runDefaultTests(screenshotCmd, PlayerCommand.serviceName);
commandHandlerTestHelper.runDefaultTests(screenshotCmd, 'screenshot');
it('sends the screenshot if the Promise on the screenshot service resolves', function() {
var contents = 'data:image/svg+xml;utf8,';
var devicePostUrl = '/foo/bar/baz';
stubScreenshotService.api.captureScreenshotAsURI.returns(Promise.resolve(contents));
stubStoreService.api.getState.returns({ statusmodel: { devicePostUrl: devicePostUrl } });
var settings = {
method: 'POST',
data: { data: contents }
};
return screenshotCmd.handleCommand('screenshot').then(function() {
expect(stubNetworkService.api.ajax).to.have.been.calledWithExactly(devicePostUrl + '.screenshot.json', settings);
});
});
it('logs an error if the Promise on the screenshot service rejects', function(done) {
var err = new Error('error');
stubScreenshotService.api.captureScreenshotAsURI.returns(Promise.reject(err));
stubStoreService.api.getState.returns({ statusmodel: { devicePostUrl: '/foo/bar/baz' } });
screenshotCmd.handleCommand('screenshot').catch(function() {
expect(stubLoggerService.api.getWriter().error).to.have.been.calledWithExactly(err);
done();
});
});
it('logs the device ID when sending the screenshot', function() {
stubScreenshotService.api.captureScreenshotAsURI.returns(Promise.resolve());
stubStoreService.api.getState.returns({ statusmodel: { devicePostUrl: '/foo/bar/baz' } });
return screenshotCmd.handleCommand('screenshot').then(function() {
expect(stubLoggerService.api.getWriter().log).to.have.been.calledWithExactly('Sending screenshot for ' + deviceId);
});
});
it('logs an error and rejects if the devicePostUrl is missing in the state', function(done) {
stubStoreService.api.getState.returns({ statusmodel: {} });
screenshotCmd.handleCommand('screenshot').catch(function() {
expect(stubLoggerService.api.getWriter().error).to.have.been.called;
done();
});
});
it('throws an error and does nothing on an missing command', function() {
expect(function() { screenshotCmd.handleCommand(); }).to.throw();
expect(stubNetworkService.api.ajax).to.not.have.been.called;
});
it('throws an error and does nothing on an invalid command', function() {
expect(function() { screenshotCmd.handleCommand('invalid'); }).to.throw();
expect(stubNetworkService.api.ajax).to.not.have.been.called;
});
});
});