/
frameworkSettingsSagas.spec.ts
164 lines (155 loc) · 5.6 KB
/
frameworkSettingsSagas.spec.ts
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Bot Framework: http://botframework.com
//
// Bot Framework Emulator Github:
// https://github.com/Microsoft/BotFramwork-Emulator
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import { newNotification, SharedConstants, FrameworkSettings } from '@bfemulator/app-shared';
import { applyMiddleware, combineReducers, createStore } from 'redux';
import sagaMiddlewareFactory from 'redux-saga';
import { call, put, select, takeEvery } from 'redux-saga/effects';
import { CommandServiceImpl, CommandServiceInstance } from '@bfemulator/sdk-shared';
import { CONTENT_TYPE_APP_SETTINGS, DOCUMENT_ID_APP_SETTINGS } from '../../constants';
import * as EditorActions from '../actions/editorActions';
import {
FrameworkActionType,
saveFrameworkSettings as saveFrameworkSettingsAction,
setFrameworkSettings,
} from '../actions/frameworkSettingsActions';
import { beginAdd } from '../actions/notificationActions';
import { editor } from '../reducers/editor';
import { framework } from '../reducers/framework';
import {
activeDocumentSelector,
frameworkSettingsSagas,
getFrameworkSettings,
FrameworkSettingsSagas,
} from './frameworkSettingsSagas';
jest.mock('electron', () => ({
ipcMain: new Proxy(
{},
{
get(): any {
return () => ({});
},
has() {
return true;
},
}
),
ipcRenderer: new Proxy(
{},
{
get(): any {
return () => ({});
},
has() {
return true;
},
}
),
}));
const sagaMiddleWare = sagaMiddlewareFactory();
const mockStore = createStore(combineReducers({ framework, editor }), {}, applyMiddleware(sagaMiddleWare));
sagaMiddleWare.run(frameworkSettingsSagas);
jest.mock('../store', () => ({
get store() {
return mockStore;
},
}));
mockStore.dispatch(
EditorActions.open({
contentType: CONTENT_TYPE_APP_SETTINGS,
documentId: DOCUMENT_ID_APP_SETTINGS,
isGlobal: true,
meta: null,
})
);
describe('The frameworkSettingsSagas', () => {
let commandService: CommandServiceImpl;
beforeAll(() => {
const decorator = CommandServiceInstance();
const descriptor = decorator({ descriptor: {} }, 'none') as any;
commandService = descriptor.descriptor.get();
});
it('should register the expected generators', () => {
const gen = frameworkSettingsSagas();
expect(gen.next().value).toEqual(
takeEvery(FrameworkActionType.SAVE_FRAMEWORK_SETTINGS, FrameworkSettingsSagas.saveFrameworkSettings)
);
});
it('should save the framework settings', async () => {
const currentSettings: Partial<FrameworkSettings> = {
autoUpdate: false,
useCustomId: false,
usePrereleases: false,
userGUID: '',
ngrokPath: 'some/path/to/ngrok',
};
const updatedSettings: Partial<FrameworkSettings> = {
autoUpdate: true,
useCustomId: true,
usePrereleases: false,
userGUID: 'some-user-id',
ngrokPath: 'some/different/path/to/ngrok',
};
const gen = FrameworkSettingsSagas.saveFrameworkSettings(saveFrameworkSettingsAction(updatedSettings));
// selector to get the active document from the state
const selector = gen.next().value;
expect(selector).toEqual(select(activeDocumentSelector));
const value = selector.SELECT.selector(mockStore.getState());
// put the dirty state to false
expect(gen.next(value).value).toEqual(put(EditorActions.setDirtyFlag(value.documentId, false)));
expect(gen.next().value).toEqual(put(setFrameworkSettings(updatedSettings)));
expect(gen.next().value).toEqual(select(getFrameworkSettings));
expect(gen.next(currentSettings).value).toEqual(
call(
[commandService, commandService.remoteCall],
SharedConstants.Commands.Telemetry.TrackEvent,
'app_changeSettings',
{
autoUpdate: true,
useCustomId: true,
userGUID: 'some-user-id',
ngrokPath: 'some/different/path/to/ngrok',
}
)
);
expect(gen.next().done).toBe(true);
});
it('should send a notification when saving the settings fails', () => {
const gen = FrameworkSettingsSagas.saveFrameworkSettings(saveFrameworkSettingsAction({}));
gen.next();
const errMsg = `Error while saving emulator settings: oh noes!`;
const notification = newNotification(errMsg);
notification.timestamp = jasmine.any(Number) as any;
notification.id = jasmine.any(String) as any;
expect(gen.throw('oh noes!').value).toEqual(put(beginAdd(notification)));
});
});