Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/pull-request-list/pull-request-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/pull-request-list/pull-request-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<typeof pullRequestNotifications>
let stubs: Mocks<Dependencies>
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'
)
})
})
Original file line number Diff line number Diff line change
@@ -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)
)
Expand All @@ -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 {
Expand Down