Skip to content

Commit 42b01b6

Browse files
committed
Added unit tests for notification actions
1 parent 468a07c commit 42b01b6

File tree

12 files changed

+286
-13
lines changed

12 files changed

+286
-13
lines changed

src/main/typescript/notifications/actions/MarkAsReadAction.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ export class MarkAsReadAction implements NotificationAction {
3232
}
3333

3434
private readSingle(notification: GitHub.Thread): void {
35-
this.gitHubClient
36-
.markThreadAsRead(notification)
37-
.then(() => {
38-
return this.eventDispatcher.emit('notificationRead', notification.id);
39-
})
35+
void Promise.resolve()
36+
.then(() => this.gitHubClient.markThreadAsRead(notification))
37+
.then(() => this.eventDispatcher.emit('notificationRead', notification.id))
4038
.catch((error) => {
4139
if (error instanceof ApiError) {
4240
MarkAsReadAction.LOGGER.error(
@@ -53,11 +51,9 @@ export class MarkAsReadAction implements NotificationAction {
5351
}
5452

5553
private readAll(): void {
56-
this.gitHubClient
57-
.markAllThreadsAsRead()
58-
.then(() => {
59-
return this.eventDispatcher.emit('allNotificationsRead');
60-
})
54+
void Promise.resolve()
55+
.then(() => this.gitHubClient.markAllThreadsAsRead())
56+
.then(() => this.eventDispatcher.emit('allNotificationsRead'))
6157
.catch((error) => {
6258
if (error instanceof ApiError) {
6359
MarkAsReadAction.LOGGER.error(

src/main/typescript/notifications/actions/OpenAction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export class OpenAction implements NotificationAction {
3535
}
3636

3737
private openSingle(notification: GitHub.Thread): void {
38-
this.gitHubClient
39-
.getWebUrlForSubject(notification.subject)
38+
void Promise.resolve()
39+
.then(() => this.gitHubClient.getWebUrlForSubject(notification.subject))
4040
.then((url) => show_uri(null, url, CURRENT_TIME))
4141
.then(() => this.eventDispatcher.emit('notificationRead', notification.id))
4242
.catch((error) => {

src/test/stubs/gi-types/clutter10.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export let CURRENT_TIME: number = new Date().getTime()

src/test/stubs/gi-types/gtk4.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function show_uri(parent: Window | null, uri: string, timestamp: number): void {}

src/test/stubs/gi-types/st1.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class Widget {}
2+
3+
export class BoxLayout {}

src/test/stubs/gnome-shell/misc/extensionUtils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Settings } from '@gi-types/gio2';
2+
13
export class ExtensionMetadata {
24
uuid: string = 'e1f0a5eb-a65b-4dff-a0c2-ba024ac7ea3a';
35
name: string = 'TestSuite';
@@ -28,3 +30,9 @@ export function getCurrentExtension(): Extension {
2830
ext.metadata.name = 'TestSuite';
2931
return ext;
3032
};
33+
34+
export function openPrefs(): void {};
35+
36+
export function getSettings(name?: string): Settings {
37+
return new Settings();
38+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { MessageTray } from './ui/messageTray';
2+
import { Panel } from './ui/panel';
3+
4+
export class Main {
5+
panel: Panel = new Panel();
6+
messageTray: MessageTray = new MessageTray();
7+
}
8+
9+
export const main: Main = new Main();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { BoxLayout } from '@gi-types/st1';
2+
3+
import { Button } from './panelMenu';
4+
5+
export class Panel {
6+
_rightBox: BoxLayout = new BoxLayout();
7+
8+
public addToStatusArea(role: string, indicator: Button, position?: number, box?: number): Button {
9+
return new Button();
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Widget } from "@gi-types/st1";
2+
3+
export class ButtonBox {}
4+
5+
export class Button extends ButtonBox {
6+
7+
public constructor(_menuAlignment?: number, _nameText?: string, _dontCreateMenu?: boolean) {
8+
super();
9+
}
10+
11+
public add_actor(widget: Widget): void {}
12+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { readFileSync } from 'fs';
2+
3+
import { GitHubClientStub, assertWithRetries, testResource } from '@test-suite/testSupport';
4+
import { assert } from 'chai';
5+
import { spy, stub } from 'sinon';
6+
7+
import * as GitHub from '@github-manager/client/GitHubApiTypes';
8+
import { GitHubClient } from '@github-manager/client/GitHubClient';
9+
import { MarkAsReadAction } from '@github-manager/notifications/actions/MarkAsReadAction';
10+
import { EventDispatcher } from '@github-manager/utils';
11+
12+
describe('Mark As Read Action', () => {
13+
let thread: GitHub.Thread;
14+
let action: MarkAsReadAction;
15+
16+
let githubClient: GitHubClient;
17+
let eventDispatcher: EventDispatcher;
18+
19+
before(() => {
20+
const scenarioJSON = readFileSync(testResource('../notifications.json'), { encoding: 'utf-8' });
21+
thread = (JSON.parse(scenarioJSON) as GitHub.Thread[])[0];
22+
});
23+
24+
beforeEach(() => {
25+
githubClient = new GitHubClientStub();
26+
eventDispatcher = new EventDispatcher();
27+
28+
action = new MarkAsReadAction(githubClient, eventDispatcher);
29+
});
30+
31+
describe('On a single notification', () => {
32+
it('can mark it as read', () => {
33+
const markThreadAsReadSpy = spy(githubClient, 'markThreadAsRead');
34+
const emitSpy = spy(eventDispatcher, 'emit');
35+
36+
action.execute(thread);
37+
38+
void assertWithRetries('Notification is correctly marked as read', 10, 150, () => {
39+
assert.equal(markThreadAsReadSpy.callCount, 1);
40+
assert.equal(markThreadAsReadSpy.firstCall.args.length, 1);
41+
assert.equal(markThreadAsReadSpy.firstCall.firstArg, thread);
42+
43+
assert.equal(emitSpy.callCount, 1);
44+
assert.equal(emitSpy.firstCall.args.length, 2);
45+
assert.equal(emitSpy.firstCall.args[0], 'notificationRead');
46+
assert.equal(emitSpy.firstCall.args[1], thread.id);
47+
});
48+
});
49+
50+
it('does nothing if GitHub call fails', () => {
51+
const emitSpy = spy(eventDispatcher, 'emit');
52+
stub(githubClient, 'markThreadAsRead').throws(new Error('Api Error'));
53+
54+
assert.doesNotThrow(() => action.execute(thread));
55+
assert.isTrue(emitSpy.notCalled);
56+
});
57+
});
58+
59+
describe('On all notifications', () => {
60+
it('can mark them as read', () => {
61+
const markAllThreadsAsReadSpy = spy(githubClient, 'markAllThreadsAsRead');
62+
const emitSpy = spy(eventDispatcher, 'emit');
63+
64+
action.execute();
65+
66+
void assertWithRetries('All notifications are marked as read', 10, 150, () => {
67+
assert.equal(markAllThreadsAsReadSpy.callCount, 1);
68+
assert.equal(markAllThreadsAsReadSpy.firstCall.args.length, 0);
69+
70+
assert.equal(emitSpy.callCount, 1);
71+
assert.equal(emitSpy.firstCall.args.length, 1);
72+
assert.equal(emitSpy.firstCall.args[0], 'allNotificationsRead');
73+
});
74+
});
75+
76+
it('does nothing if GitHub call fails', () => {
77+
const emitSpy = spy(eventDispatcher, 'emit');
78+
stub(githubClient, 'markAllThreadsAsRead').throws(new Error('Api Error'));
79+
80+
assert.doesNotThrow(() => action.execute());
81+
assert.isTrue(emitSpy.notCalled);
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)