|
| 1 | +/* |
| 2 | + * Copyright SeatGeek |
| 3 | + * Licensed under the terms of the Apache-2.0 license. See LICENSE file in project root for terms. |
| 4 | + */ |
| 5 | +import { Award } from '@seatgeek/backstage-plugin-awards-common'; |
| 6 | +import * as winston from 'winston'; |
| 7 | +import { Awards } from './awards'; |
| 8 | +import { AwardsStore } from './database/awards'; |
| 9 | +import { AwardsNotifier } from './notifier'; |
| 10 | + |
| 11 | +const frank = 'user:default/frank-ocean'; |
| 12 | + |
| 13 | +function makeAward(): Award { |
| 14 | + return { |
| 15 | + uid: '123456', |
| 16 | + name: 'Test Award', |
| 17 | + description: 'This is a test award', |
| 18 | + image: 'image_data', |
| 19 | + owners: [frank], |
| 20 | + recipients: ['user:default/peyton-manning', 'user:default/serena-williams'], |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +describe('Awards', () => { |
| 25 | + let db: jest.Mocked<AwardsStore>; |
| 26 | + let notifier: jest.Mocked<AwardsNotifier>; |
| 27 | + let awards: Awards; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + db = { |
| 31 | + search: jest.fn(), |
| 32 | + add: jest.fn(), |
| 33 | + update: jest.fn(), |
| 34 | + delete: jest.fn(), |
| 35 | + }; |
| 36 | + notifier = { |
| 37 | + notifyNewRecipients: jest.fn(), |
| 38 | + }; |
| 39 | + const logger = winston.createLogger({ |
| 40 | + transports: [new winston.transports.Console({ silent: true })], |
| 41 | + }); |
| 42 | + awards = new Awards(db, notifier, logger); |
| 43 | + }); |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + jest.resetAllMocks(); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('create', () => { |
| 50 | + it('should notify new recipients', async () => { |
| 51 | + const award = makeAward(); |
| 52 | + db.add = jest.fn().mockResolvedValue(award); |
| 53 | + const result = await awards.create({ |
| 54 | + name: award.name, |
| 55 | + description: award.description, |
| 56 | + image: award.image, |
| 57 | + owners: award.owners, |
| 58 | + recipients: award.recipients, |
| 59 | + }); |
| 60 | + |
| 61 | + // wait for the afterCreate promises to complete |
| 62 | + await new Promise(process.nextTick); |
| 63 | + |
| 64 | + expect(result).toEqual(award); |
| 65 | + expect(db.add).toHaveBeenCalledWith( |
| 66 | + award.name, |
| 67 | + award.description, |
| 68 | + award.image, |
| 69 | + award.owners, |
| 70 | + award.recipients, |
| 71 | + ); |
| 72 | + expect(notifier.notifyNewRecipients).toHaveBeenCalledWith(award, [ |
| 73 | + 'user:default/peyton-manning', |
| 74 | + 'user:default/serena-williams', |
| 75 | + ]); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('update', () => { |
| 80 | + it('should notify new recipients', async () => { |
| 81 | + const award = makeAward(); |
| 82 | + db.search = jest.fn().mockResolvedValue([award]); |
| 83 | + const updated = { |
| 84 | + ...award, |
| 85 | + recipients: [ |
| 86 | + ...award.recipients, |
| 87 | + 'user:default/megan-rapinoe', |
| 88 | + 'user:default/adrianne-lenker', |
| 89 | + ], |
| 90 | + }; |
| 91 | + db.update = jest.fn().mockResolvedValue(updated); |
| 92 | + const result = await awards.update(frank, award.uid, updated); |
| 93 | + |
| 94 | + // wait for the afterUpdate promises to complete |
| 95 | + await new Promise(process.nextTick); |
| 96 | + |
| 97 | + expect(result).toEqual(updated); |
| 98 | + expect(db.update).toHaveBeenCalledWith( |
| 99 | + updated.uid, |
| 100 | + updated.name, |
| 101 | + updated.description, |
| 102 | + updated.image, |
| 103 | + updated.owners, |
| 104 | + updated.recipients, |
| 105 | + ); |
| 106 | + expect(notifier.notifyNewRecipients).toHaveBeenCalledWith(updated, [ |
| 107 | + 'user:default/megan-rapinoe', |
| 108 | + 'user:default/adrianne-lenker', |
| 109 | + ]); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments