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.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 2f3e3845..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' +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.js b/src/services/pull-request-notifications/pull-request-notifications.ts similarity index 62% rename from src/services/pull-request-notifications/pull-request-notifications.js rename to src/services/pull-request-notifications/pull-request-notifications.ts index ff656468..ef1a5ff2 100644 --- a/src/services/pull-request-notifications/pull-request-notifications.js +++ b/src/services/pull-request-notifications/pull-request-notifications.ts @@ -1,10 +1,12 @@ import notificationService from '../notifications/notification' +type PullRequest = { title: string; url: string } const defaults = { notificationApi: notificationService } -const pullRequestNotifications = ({ notificationApi } = defaults) => { - let alreadyNotified = [] +export type Dependencies = { notificationApi: typeof notificationService } +const pullRequestNotifications = ({ notificationApi }: Dependencies = defaults) => { + let alreadyNotified: PullRequest[] = [] - const newList = (pullRequests) => { + const newList = (pullRequests: PullRequest[]) => { const prToNotify = pullRequests.filter( ({ title }) => !alreadyNotified.find((notifiedPr) => notifiedPr.title === title) ) @@ -15,8 +17,8 @@ const pullRequestNotifications = ({ notificationApi } = defaults) => { } } - const formatNotificationMessage = (prToNotify) => { - if (prToNotify.length === 1) { + const formatNotificationMessage = (prToNotify: PullRequest[]) => { + if (prToNotify.length === 1 && prToNotify[0]) { const [{ title }] = prToNotify return `A new pull request was opened: ${title}` } else {