Created
May 17, 2022 14:40
-
-
Save dereknahman/dfeb84671d178b03e954b617cced2d7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mongoose from 'mongoose'; | |
import googleDriveUpload from '../utils/googleDriveUpload'; | |
import sendEmail from '../utils/sendEmail'; | |
import updatePaidCommission from '../routes/payments/sendRequest/updatePaidCommission'; | |
import createAccountantsSheet from '../routes/payments/spreadsheets/accountReporting'; | |
import { setupFakeData, cleanupFakeData } from '../../../test/setupFakeData'; | |
import sendStaffPayments from './sendStaffPayments'; | |
import Scheduler from '../db/models/scheduler'; | |
const mockCutOff = jest.fn(); | |
jest.mock('../utils/sendEmail'); | |
jest.mock('../utils/googleDriveUpload'); | |
jest.mock('../routes/payments/spreadsheets', () => ({ | |
staff: () => 'staff csv output', | |
})); | |
jest.mock('../routes/payments/sendRequest/updatePaidCommission'); | |
jest.mock('../routes/payments/spreadsheets/accountReporting'); | |
jest.mock('../../shared/utils/dateUtils', () => ({ | |
today: () => '2021-07-13', | |
paymentCutOffDate: () => mockCutOff(), | |
})); | |
let spyScheduler; | |
describe('Do not send staff payments when there are no payments', () => { | |
beforeAll(async () => { | |
await mongoose.connect(global.__MONGO_URI__, { | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
}); | |
}); | |
afterAll(async () => { | |
try { | |
await mongoose.connection.close(); | |
} catch (err) { | |
console.log('Error closing Mongoose connection after tests', err); // eslint-disable-line no-console | |
} | |
}); | |
beforeEach(async () => { | |
process.env.EMAIL_GROUP_IEXPENSES = 'test-finance-email'; | |
process.env.EMAIL_GROUP_PAYROLL_UK = 'uk-test-payroll-email'; | |
process.env.EMAIL_GROUP_PAYROLL_US = 'us-test-payroll-email'; | |
process.env.EMAIL_GROUP_EDITORIAL_ASSISTANTS = 'test-editorial-email'; | |
process.env.GOOGLE_DRIVE_PAYMENT_FOLDER_STAFF_UK = 'UK-folder'; | |
process.env.GOOGLE_DRIVE_PAYMENT_FOLDER_STAFF_US = 'US-folder'; | |
googleDriveUpload.mockResolvedValue({ | |
location: 'uploaded-filename.csv', | |
}); | |
mockCutOff.mockRestore(); | |
updatePaidCommission.mockRestore(); | |
jest.restoreAllMocks(); | |
mockCutOff.mockReturnValue('2021-01-01'); | |
spyScheduler = await jest.spyOn(Scheduler, 'updateOne'); | |
await sendStaffPayments(); | |
}); | |
it('Does not send the staff payment spreadsheets when there are no payments', () => { | |
expect(createAccountantsSheet).not.toHaveBeenCalledWith({ | |
UK: expect.arrayContaining([ | |
expect.objectContaining({ | |
_id: expect.anything(), | |
contributor: expect.objectContaining({ | |
employeePayrollGroup: 'UK', | |
}), | |
}), | |
]), | |
US: expect.arrayContaining([ | |
expect.objectContaining({ | |
_id: expect.anything(), | |
contributor: expect.objectContaining({ | |
employeePayrollGroup: 'US', | |
}), | |
}), | |
]), | |
}); | |
}); | |
}); | |
describe('Send staff payment when payments exist', () => { | |
beforeAll(async () => { | |
await mongoose.connect(global.__MONGO_URI__, { | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
}); | |
await setupFakeData(); | |
}); | |
afterAll(async () => { | |
try { | |
await cleanupFakeData(); | |
await mongoose.connection.close(); | |
} catch (err) { | |
console.log('Error cleaning up after tests', err); // eslint-disable-line no-console | |
} | |
}); | |
beforeEach(async () => { | |
process.env.EMAIL_GROUP_IEXPENSES = 'test-finance-email'; | |
process.env.EMAIL_GROUP_PAYROLL_UK = 'uk-test-payroll-email'; | |
process.env.EMAIL_GROUP_PAYROLL_US = 'us-test-payroll-email'; | |
process.env.EMAIL_GROUP_EDITORIAL_ASSISTANTS = 'test-editorial-email'; | |
process.env.GOOGLE_DRIVE_PAYMENT_FOLDER_STAFF_UK = 'UK-folder'; | |
process.env.GOOGLE_DRIVE_PAYMENT_FOLDER_STAFF_US = 'US-folder'; | |
googleDriveUpload.mockResolvedValue({ | |
location: 'uploaded-filename.csv', | |
}); | |
mockCutOff.mockRestore(); | |
updatePaidCommission.mockRestore(); | |
jest.restoreAllMocks(); | |
mockCutOff.mockReturnValue('2021-01-01'); | |
spyScheduler = await jest.spyOn(Scheduler, 'updateOne'); | |
await sendStaffPayments(); | |
}); | |
afterEach(() => { | |
jest.resetAllMocks(); | |
}); | |
it('Uploads the CSV file to google drive for each payment group', () => { | |
const googleDriveModel = (group) => ({ | |
contents: 'staff csv output', | |
filename: `Contributor Payments ${group} 2021-07-13.csv`, | |
folderId: `${group}-folder`, | |
type: 'staff', | |
}); | |
const [[ukUpload], [usUpload]] = googleDriveUpload.mock.calls; | |
expect(ukUpload).toEqual(googleDriveModel('UK')); | |
expect(usUpload).toEqual(googleDriveModel('US')); | |
}); | |
it('emails a link to the CSV for each payment group', () => { | |
['UK', 'US'].forEach((group, i) => | |
expect(sendEmail.mock.calls[i][0]).toEqual( | |
expect.objectContaining({ | |
replyTo: 'test-finance-email', | |
subject: `Payroll Advice (FT Staff - ${group}) for 2021-07-13`, | |
to: [expect.stringContaining('test-payroll-email')], | |
}) | |
) | |
); | |
}); | |
it('updates the paymentRequested and paid fields on the commission for each payment group', () => { | |
expect(updatePaidCommission.mock.calls[0][0]).toEqual({ | |
ids: expect.any(Array), | |
location: 'uploaded-filename.csv', | |
markAsPaid: true, | |
paymentEvent: 'payment sent to Payroll', | |
username: 'scheduled.send', | |
}); | |
expect(updatePaidCommission.mock.calls[1][0]).toEqual({ | |
ids: expect.any(Array), | |
location: 'uploaded-filename.csv', | |
markAsPaid: true, | |
paymentEvent: 'payment sent to Payroll', | |
username: 'scheduled.send', | |
}); | |
}); | |
it('compute the next run date', () => { | |
expect(mockCutOff).toHaveBeenCalled(); | |
}); | |
it('update the database with next run run date', async () => { | |
const [[updateFilter, updatePayload]] = spyScheduler.mock.calls; | |
expect(updateFilter).toEqual({ taskType: 'SEND_PAYMENT' }); | |
expect(updatePayload).toEqual( | |
expect.objectContaining({ | |
nextRunDate: '2021-01-01', | |
taskType: 'SEND_PAYMENT', | |
}) | |
); | |
}); | |
it('should save next run date', async () => { | |
const scheduledNextRunDate = await Scheduler.findOne({ | |
taskType: 'SEND_PAYMENT', | |
}); | |
expect(scheduledNextRunDate.nextRunDate).toEqual( | |
new Date('2021-01-01T00:00:00.000Z') | |
); | |
}); | |
it('should create financial reporting spread sheet', () => { | |
expect(createAccountantsSheet).toHaveBeenCalledWith({ | |
UK: expect.arrayContaining([ | |
expect.objectContaining({ | |
_id: expect.anything(), | |
contributor: expect.objectContaining({ | |
employeePayrollGroup: 'UK', | |
}), | |
}), | |
]), | |
US: expect.arrayContaining([ | |
expect.objectContaining({ | |
_id: expect.anything(), | |
contributor: expect.objectContaining({ | |
employeePayrollGroup: 'US', | |
}), | |
}), | |
]), | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment