Skip to content

Commit

Permalink
♻️ separation of updates and notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarsson committed Mar 21, 2023
1 parent 14643c5 commit b59920b
Show file tree
Hide file tree
Showing 21 changed files with 341 additions and 372 deletions.
262 changes: 262 additions & 0 deletions src/about-me/person/notifications/__test/make-person-notifier.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
import { Email, Person, PersonNotifier, Phone } from '../../types'
import { makePersonNotifier } from '../make-person-notifier'

const notImplemented = () => { throw new Error('not implemented') }

describe('makePersonNotifier().notifyPersonUpdates() -> notifyPhoneChanged()', () => {
const captureNotifications = async (initialPerson: Person, updatedPerson: Person): Promise<Phone[]> => {
const nofificationLog: Phone[] = []
const fakeNotifier: PersonNotifier = makePersonNotifier({
notifyEmailChanged: notImplemented,
notifyPhoneChanged: async (phone?: Phone) => (phone && nofificationLog.push(phone), true),
})
await fakeNotifier.notifyPersonUpdates(initialPerson, updatedPerson)
return nofificationLog
}

it('notifies when phone number is added', async () => {
const log = await captureNotifications(
{
id: 'test-person-123',
},
{
id: 'test-person-123',
phone: {
number: '+46721234567',
isVerified: false,
verificationCode: 'vc-1234',
},
}
)
expect(log).toMatchObject([
{
number: '+46721234567',
verificationCode: 'vc-1234',
},
])
})

it('notifies when phone number is changed', async () => {
const log = await captureNotifications(
{
id: 'test-person-123',
phone: {
number: '000-111',
},
},
{
id: 'test-person-123',
phone: {
number: '+46721234567',
isVerified: false,
verificationCode: 'vc-1234',
},
}
)
expect(log).toMatchObject([
{
number: '+46721234567',
verificationCode: 'vc-1234',
},
])
})
it('does nothing if phone number is not set', async () => {
const log = await captureNotifications(
{
id: 'test-person-123',
},
{
id: 'test-person-123',
}
)
expect(log).toHaveLength(0)
})

it('does nothing is phone number is unchanged', async () => {
const log = await captureNotifications(
{
id: 'test-person-123',
phone: {
number: '+721234567',
},
},
{
id: 'test-person-123',
phone: {
number: '+721234567',
isVerified: false,
verificationCode: 'vc-1234',
},
}
)
expect(log).toHaveLength(0)
})
it('does nothing if new phone number is verified', async () => {
const log = await captureNotifications(
{
id: 'test-person-123',
phone: {
number: '+721234567',
},
},
{
id: 'test-person-123',
phone: {
number: '+721234567',
isVerified: true,
verificationCode: 'vc-1234',
},
}
)
expect(log).toHaveLength(0)
})
it('does nothing if verificationCode is missing', async () => {
const log = await captureNotifications(
{
id: 'test-person-123',
phone: {
number: '000-111',
},
},
{
id: 'test-person-123',
phone: {
number: '+721234567',
isVerified: false,
verificationCode: '',
},
}
)
expect(log).toHaveLength(0)
})
})

describe('makePersonNotifier().notifyPersonUpdates() -> notifyEmailChanged()', () => {

const captureNotifications = async (initialPerson: Person, updatedPerson: Person): Promise<Email[]> => {
const nofificationLog: Email[] = []
const fakeNotifier: PersonNotifier = makePersonNotifier({
notifyEmailChanged: async (email?: Email) => (email && nofificationLog.push(email), true),
notifyPhoneChanged: notImplemented,
})
await fakeNotifier.notifyPersonUpdates(initialPerson, updatedPerson)
return nofificationLog
}

it('notifies when email is added', async () => {
const log = await captureNotifications(
{
id: 'test-person-123',
},
{
id: 'test-person-123',
email: {
address: 'a@b.com',
isVerified: false,
verificationCode: 'vc-1234',
},
}
)
expect(log).toMatchObject([
{
address: 'a@b.com',
verificationCode: 'vc-1234',
},
])
})

it('notifies when email is changed', async () => {
const log = await captureNotifications(
{
id: 'test-person-123',
email: {
address: 'to@be.replaced',
},
},
{
id: 'test-person-123',
email: {
address: 'a@b.com',
isVerified: false,
verificationCode: 'vc-1234',
},
}
)
expect(log).toMatchObject([
{
address: 'a@b.com',
verificationCode: 'vc-1234',
},
])
})
it('does nothing if mail is not set', async () => {
const log = await captureNotifications(
{
id: 'test-person-123',
},
{
id: 'test-person-123',
}
)
expect(log).toHaveLength(0)
})

it('does nothing is mail is unchanged', async () => {
const log = await captureNotifications(
{
id: 'test-person-123',
email: {
address: 'a@b.com',
},
},
{
id: 'test-person-123',
email: {
address: 'a@b.com',
isVerified: false,
verificationCode: 'vc-1234',
},
}
)
expect(log).toHaveLength(0)
})
it('does nothing if new mail is verified', async () => {
const log = await captureNotifications(
{
id: 'test-person-123',
email: {
address: 'a@b.com',
},
},
{
id: 'test-person-123',
email: {
address: 'updated@b.com',
isVerified: true,
verificationCode: 'vc-1234',
},
}
)
expect(log).toHaveLength(0)
})
it('does nothing if verificationCode is missing', async () => {
const log = await captureNotifications(
{
id: 'test-person-123',
email: {
address: 'a@b.com',
},
},
{
id: 'test-person-123',
email: {
address: 'updated@b.com',
isVerified: false,
verificationCode: '',
},
}
)
expect(log).toHaveLength(0)
})

})
Loading

0 comments on commit b59920b

Please sign in to comment.