From bc4c4b8308112465fe11554d926dfc49a48bcf23 Mon Sep 17 00:00:00 2001 From: udebella Date: Fri, 26 Dec 2025 18:19:17 +0100 Subject: [PATCH 1/2] Migrate notifications to typescript --- .../pull-request-list/pull-request-list.spec.ts | 2 +- src/components/pull-request-list/pull-request-list.vue | 2 +- .../pull-request-notifications.spec.js | 2 +- ...st-notifications.js => pull-request-notifications.ts} | 9 +++++---- 4 files changed, 8 insertions(+), 7 deletions(-) rename src/services/pull-request-notifications/{pull-request-notifications.js => pull-request-notifications.ts} (76%) diff --git a/src/components/pull-request-list/pull-request-list.spec.ts b/src/components/pull-request-list/pull-request-list.spec.ts index d1a7f14f..9525d220 100644 --- a/src/components/pull-request-list/pull-request-list.spec.ts +++ b/src/components/pull-request-list/pull-request-list.spec.ts @@ -7,7 +7,7 @@ import PullRequestLine from '../ui/pull-request-line/pull-request-line.vue' import type { Mocks, Wrapper } from '../../test-utils.ts' import { buildRepositoriesQuery } from '../../services/graphql/query-builder.ts' import { extractHttp as extractPullRequest, type GDPullRequest } from '../../services/pull-request/pull-request.ts' -import type { pullRequestNotifications } from '../../services/pull-request-notifications/pull-request-notifications' +import type { pullRequestNotifications } from '../../services/pull-request-notifications/pull-request-notifications.ts' import type { buildUserService } from '../../services/user/user' type Dependencies = { diff --git a/src/components/pull-request-list/pull-request-list.vue b/src/components/pull-request-list/pull-request-list.vue index 5e4b61eb..69e61dfe 100644 --- a/src/components/pull-request-list/pull-request-list.vue +++ b/src/components/pull-request-list/pull-request-list.vue @@ -38,7 +38,7 @@ import { import { buildUserService } from '../../services/user/user' import { useRepositoryStore } from '../../stores/repositories/repositories' import { computed, inject, ref } from 'vue' -import { pullRequestNotifications as defaultPullRequestNotifications } from '../../services/pull-request-notifications/pull-request-notifications' +import { pullRequestNotifications as defaultPullRequestNotifications } from '../../services/pull-request-notifications/pull-request-notifications.ts' import { NO_USER } from '../../services/session/session.ts' const pullRequestListFragment = `${pullRequestFragment} diff --git a/src/services/pull-request-notifications/pull-request-notifications.spec.js b/src/services/pull-request-notifications/pull-request-notifications.spec.js index 2f3e3845..7c3555f3 100644 --- a/src/services/pull-request-notifications/pull-request-notifications.spec.js +++ b/src/services/pull-request-notifications/pull-request-notifications.spec.js @@ -1,4 +1,4 @@ -import { pullRequestNotifications } from './pull-request-notifications' +import { pullRequestNotifications } from './pull-request-notifications.ts' import { beforeEach, describe, expect, it, vitest } from 'vitest' describe('Pull request notification service', () => { diff --git a/src/services/pull-request-notifications/pull-request-notifications.js b/src/services/pull-request-notifications/pull-request-notifications.ts similarity index 76% rename from src/services/pull-request-notifications/pull-request-notifications.js rename to src/services/pull-request-notifications/pull-request-notifications.ts index ff656468..a11100f4 100644 --- a/src/services/pull-request-notifications/pull-request-notifications.js +++ b/src/services/pull-request-notifications/pull-request-notifications.ts @@ -1,10 +1,11 @@ import notificationService from '../notifications/notification' +type PullRequest = { title: string; url: string } const defaults = { notificationApi: notificationService } const pullRequestNotifications = ({ notificationApi } = defaults) => { - let alreadyNotified = [] + let alreadyNotified: PullRequest[] = [] - const newList = (pullRequests) => { + const newList = (pullRequests: PullRequest[]) => { const prToNotify = pullRequests.filter( ({ title }) => !alreadyNotified.find((notifiedPr) => notifiedPr.title === title) ) @@ -15,8 +16,8 @@ const pullRequestNotifications = ({ notificationApi } = defaults) => { } } - const formatNotificationMessage = (prToNotify) => { - if (prToNotify.length === 1) { + const formatNotificationMessage = (prToNotify: PullRequest[]) => { + if (prToNotify[0]) { const [{ title }] = prToNotify return `A new pull request was opened: ${title}` } else { From acac124a30204995fb8977ff2151fe63e4b5c88c Mon Sep 17 00:00:00 2001 From: udebella Date: Fri, 26 Dec 2025 18:29:59 +0100 Subject: [PATCH 2/2] Migrate tests to typescript --- ....js => pull-request-notifications.spec.ts} | 41 +++++++++---------- .../pull-request-notifications.ts | 5 ++- 2 files changed, 23 insertions(+), 23 deletions(-) rename src/services/pull-request-notifications/{pull-request-notifications.spec.js => pull-request-notifications.spec.ts} (58%) diff --git a/src/services/pull-request-notifications/pull-request-notifications.spec.js b/src/services/pull-request-notifications/pull-request-notifications.spec.ts similarity index 58% rename from src/services/pull-request-notifications/pull-request-notifications.spec.js rename to src/services/pull-request-notifications/pull-request-notifications.spec.ts index 7c3555f3..d4213034 100644 --- a/src/services/pull-request-notifications/pull-request-notifications.spec.js +++ b/src/services/pull-request-notifications/pull-request-notifications.spec.ts @@ -1,69 +1,68 @@ -import { pullRequestNotifications } from './pull-request-notifications.ts' +import { type Dependencies, pullRequestNotifications } from './pull-request-notifications.ts' import { beforeEach, describe, expect, it, vitest } from 'vitest' +import type { Mocks } from '../../test-utils.ts' describe('Pull request notification service', () => { - let stubs + let pullRequestNotification: ReturnType + let stubs: Mocks beforeEach(() => { - const notify = vitest.fn() stubs = { notificationApi: { - notify - }, - notify + notify: vitest.fn(), + requestNotifications: vitest.fn() + } } + + pullRequestNotification = pullRequestNotifications(stubs) }) it('should not send notifications when there is no new pull request', () => { - const pullRequestNotification = pullRequestNotifications(stubs) - pullRequestNotification.newList([]) - expect(stubs.notify).not.toHaveBeenCalled() + expect(stubs.notificationApi.notify).not.toHaveBeenCalled() }) it('should send notifications when there is a new pull request', () => { - const pullRequestNotification = pullRequestNotifications(stubs) - pullRequestNotification.newList([{ title: 'Test pull request', url: '/a/random/url' }]) - expect(stubs.notify).toHaveBeenCalledWith('A new pull request was opened: Test pull request') + expect(stubs.notificationApi.notify).toHaveBeenCalledWith('A new pull request was opened: Test pull request') }) it('should send notifications when there is multiple new pull requests', () => { - const pullRequestNotification = pullRequestNotifications(stubs) - pullRequestNotification.newList([ { title: 'Test pull request', url: '/a/random/url' }, { title: 'Another test pull request', url: '/a/random/url' } ]) - expect(stubs.notify).toHaveBeenCalledWith('2 new pull requests were opened') + expect(stubs.notificationApi.notify).toHaveBeenCalledWith('2 new pull requests were opened') }) it('should send notifications only for new pull requests', () => { - const pullRequestNotification = pullRequestNotifications(stubs) pullRequestNotification.newList([{ title: 'Test pull request', url: '/a/random/url' }]) - stubs.notify.mockClear() + stubs.notificationApi.notify.mockClear() pullRequestNotification.newList([ { title: 'Test pull request', url: '/a/random/url' }, { title: 'Another test pull request', url: '/a/random/url' } ]) - expect(stubs.notify).toHaveBeenCalledWith('A new pull request was opened: Another test pull request') + expect(stubs.notificationApi.notify).toHaveBeenCalledWith( + 'A new pull request was opened: Another test pull request' + ) }) it('should keep already notified pull request', () => { - const pullRequestNotification = pullRequestNotifications(stubs) pullRequestNotification.newList([{ title: 'Test pull request', url: '/a/random/url' }]) pullRequestNotification.newList([]) - stubs.notify.mockClear() + stubs.notificationApi.notify.mockClear() pullRequestNotification.newList([ { title: 'Test pull request', url: '/a/random/url' }, { title: 'Another test pull request', url: '/a/random/url' } ]) - expect(stubs.notify).toHaveBeenCalledWith('A new pull request was opened: Another test pull request') + expect(stubs.notificationApi.notify).toHaveBeenCalledWith( + 'A new pull request was opened: Another test pull request' + ) }) }) diff --git a/src/services/pull-request-notifications/pull-request-notifications.ts b/src/services/pull-request-notifications/pull-request-notifications.ts index a11100f4..ef1a5ff2 100644 --- a/src/services/pull-request-notifications/pull-request-notifications.ts +++ b/src/services/pull-request-notifications/pull-request-notifications.ts @@ -2,7 +2,8 @@ import notificationService from '../notifications/notification' type PullRequest = { title: string; url: string } const defaults = { notificationApi: notificationService } -const pullRequestNotifications = ({ notificationApi } = defaults) => { +export type Dependencies = { notificationApi: typeof notificationService } +const pullRequestNotifications = ({ notificationApi }: Dependencies = defaults) => { let alreadyNotified: PullRequest[] = [] const newList = (pullRequests: PullRequest[]) => { @@ -17,7 +18,7 @@ const pullRequestNotifications = ({ notificationApi } = defaults) => { } const formatNotificationMessage = (prToNotify: PullRequest[]) => { - if (prToNotify[0]) { + if (prToNotify.length === 1 && prToNotify[0]) { const [{ title }] = prToNotify return `A new pull request was opened: ${title}` } else {