Skip to content

Commit d28bb18

Browse files
committed
test(notifications): unit testing for notification rendering
1 parent 98d8324 commit d28bb18

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import * as vscode from 'vscode'
7+
import * as sinon from 'sinon'
8+
import assert from 'assert'
9+
import { ToolkitNotification } from '../../notifications/types'
10+
import { panelNode } from './controller.test'
11+
import { getTestWindow } from '../shared/vscode/window'
12+
import * as VsCodeUtils from '../../shared/utilities/vsCodeUtils'
13+
import { readonlyDocument } from '../../shared/utilities/textDocumentUtilities'
14+
15+
describe('Notifications Rendering', function () {
16+
let sandbox: sinon.SinonSandbox
17+
//const panelNode: NotificationsNode = testNotificationsNode
18+
19+
beforeEach(function () {
20+
sandbox = sinon.createSandbox()
21+
})
22+
23+
afterEach(function () {
24+
sandbox.restore()
25+
})
26+
27+
// util to test txt pop-up under different senarios
28+
async function verifyTxtNotification(notification: ToolkitNotification) {
29+
const expectedContent = notification.uiRenderInstructions.content['en-US'].description
30+
const readonlyDocumentShowStub = sandbox.stub(readonlyDocument, 'show').resolves()
31+
32+
await panelNode.openNotification(notification)
33+
34+
assert.ok(readonlyDocumentShowStub.calledOnce)
35+
assert.ok(readonlyDocumentShowStub.calledWith(expectedContent, `Notification: ${notification.id}`))
36+
}
37+
38+
// util to test open url under different senarios
39+
async function verifyOpenExternalUrl(notification: ToolkitNotification) {
40+
const url = vscode.Uri.parse('https://aws.amazon.com/visualstudiocode/')
41+
//const openExternalStub = getOpenExternalStub()
42+
const openUrlStub = sandbox.stub(VsCodeUtils, 'openUrl')
43+
await panelNode.openNotification(notification)
44+
45+
assert.ok(openUrlStub.calledOnce)
46+
assert.ok(openUrlStub.calledWith(url))
47+
}
48+
49+
// test on-receive behaviors
50+
it('displays a toast with correct message on receive', async function () {
51+
const testWindow = getTestWindow()
52+
testWindow.onDidShowMessage((message) => {})
53+
54+
const notification = getToastURLTestNotification()
55+
await panelNode.onReceiveNotifications([notification])
56+
57+
const expectedMessage =
58+
notification.uiRenderInstructions.content['en-US'].toastPreview ??
59+
notification.uiRenderInstructions.content['en-US'].title
60+
61+
const shownMessages = testWindow.shownMessages
62+
assert.ok(shownMessages.some((msg) => msg.message === expectedMessage))
63+
})
64+
65+
it('displays a modal with correct buttons on receive', async function () {
66+
const testWindow = getTestWindow()
67+
const notification = getModalNotification()
68+
69+
testWindow.onDidShowMessage((message) => {
70+
const expectedButtons =
71+
notification.uiRenderInstructions.actions?.map((actions) => actions.displayText['en-US']) ?? []
72+
expectedButtons.forEach((buttonText) => {
73+
assert.ok(
74+
message.items.some((item) => item.title === buttonText),
75+
`Button "${buttonText}" is missing`
76+
)
77+
})
78+
})
79+
80+
await panelNode.onReceiveNotifications([notification])
81+
})
82+
83+
// test on-lick behaviors
84+
it('open a txt with correct content on-click', async function () {
85+
const notification = getTxtNotification()
86+
await verifyTxtNotification(notification)
87+
})
88+
89+
it('opens a URL with correct link on-click', async function () {
90+
const notification = getToastURLTestNotification()
91+
await verifyOpenExternalUrl(notification)
92+
})
93+
94+
// test modal buttons behavior
95+
it('executes updateAndReload type button', async function () {
96+
const testWindow = getTestWindow()
97+
testWindow.onDidShowMessage((message) => {
98+
// Simulate user clicking update and reload type
99+
message.selectItem('Update and Reload')
100+
})
101+
const excuteCommandStub = sandbox.stub(vscode.commands, 'executeCommand').resolves()
102+
const notification = getModalNotification()
103+
await panelNode.openNotification(notification)
104+
105+
assert.ok(excuteCommandStub.calledWith('workbench.extensions.installExtension', 'aws.toolkit.fake.extension'))
106+
assert.ok(excuteCommandStub.calledWith('workbench.action.reloadWindow'))
107+
})
108+
109+
it('executes openURL type button', async function () {
110+
const testWindow = getTestWindow()
111+
testWindow.onDidShowMessage((message) => {
112+
// Simulate user clicking open URL type
113+
message.selectItem('Proceed to Wiki')
114+
})
115+
const notification = getModalNotification()
116+
await verifyOpenExternalUrl(notification)
117+
})
118+
119+
it('executes openTxt type button', async function () {
120+
const testWindow = getTestWindow()
121+
testWindow.onDidShowMessage((message) => {
122+
// Simulate user clicking open txt type
123+
message.selectItem('Read More')
124+
})
125+
const notification = getModalNotification()
126+
await verifyTxtNotification(notification)
127+
})
128+
})
129+
130+
// generate test notifications
131+
function getToastURLTestNotification(): ToolkitNotification {
132+
return {
133+
id: 'test notification',
134+
displayIf: {
135+
extensionId: 'aws.toolkit.fake.extension',
136+
},
137+
uiRenderInstructions: {
138+
content: {
139+
[`en-US`]: {
140+
title: 'test',
141+
description: 'This is a url notification.',
142+
toastPreview: 'test toast preview',
143+
},
144+
},
145+
onRecieve: 'toast',
146+
onClick: {
147+
type: 'openUrl',
148+
url: 'https://aws.amazon.com/visualstudiocode/',
149+
},
150+
},
151+
}
152+
}
153+
154+
function getTxtNotification(): ToolkitNotification {
155+
return {
156+
id: 'test notification',
157+
displayIf: {
158+
extensionId: 'aws.toolkit.fake.extension',
159+
},
160+
uiRenderInstructions: {
161+
content: {
162+
[`en-US`]: {
163+
title: 'test',
164+
description: 'This is a text document notification.',
165+
},
166+
},
167+
onRecieve: 'toast',
168+
onClick: {
169+
type: 'openTextDocument',
170+
},
171+
},
172+
}
173+
}
174+
175+
function getModalNotification(): ToolkitNotification {
176+
return {
177+
id: 'test notification',
178+
displayIf: {
179+
extensionId: 'aws.toolkit.fake.extension',
180+
},
181+
uiRenderInstructions: {
182+
content: {
183+
[`en-US`]: {
184+
title: 'test',
185+
description: 'This is a modal notification.',
186+
},
187+
},
188+
onRecieve: 'modal',
189+
onClick: {
190+
type: 'modal',
191+
},
192+
actions: [
193+
{
194+
type: 'updateAndReload',
195+
displayText: {
196+
'en-US': 'Update and Reload',
197+
},
198+
},
199+
{
200+
type: 'openUrl',
201+
url: 'https://aws.amazon.com/visualstudiocode/',
202+
displayText: {
203+
'en-US': 'Proceed to Wiki',
204+
},
205+
},
206+
{
207+
type: 'openTxt',
208+
displayText: {
209+
'en-US': 'Read More',
210+
},
211+
},
212+
],
213+
},
214+
}
215+
}

0 commit comments

Comments
 (0)