Skip to content

Commit

Permalink
Fix: unit tests for db module (#3553)
Browse files Browse the repository at this point in the history
* isolate tests

* resolve test issues

* replace id vals

* fix unit tests
  • Loading branch information
greg-adams authored Sep 30, 2024
1 parent d650125 commit 5b98a4a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
27 changes: 14 additions & 13 deletions packages/server/__tests__/db/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const BASIC_SEARCH_CRITERIA = JSON.stringify({
});

describe('db', () => {
before(async () => {
beforeEach(async () => {
await fixtures.seed(db.knex);
});

Expand Down Expand Up @@ -272,8 +272,14 @@ describe('db', () => {

context('getAllUserSavedSearches', () => {
it('get all user saved searches', async () => {
await db.createSavedSearch({
name: 'Example search',
userId: fixtures.users.subStaffUser.id,
criteria: BASIC_SEARCH_CRITERIA,
});

const data = await db.getAllUserSavedSearches();
expect(data.length).to.equal(5);
expect(data.length).to.equal(fixtures.grantsSavedSearches.length + 1);
for (const row of data) {
expect(() => { JSON.parse(row.criteria); }).not.to.throw();
}
Expand Down Expand Up @@ -371,6 +377,8 @@ describe('db', () => {
filters: {
assignedToAgency: fixtures.users.staffUser.agency_id.toString(),
},
perPage: 50,
currentPage: 1,
});
expect(result).to.have.property('data').with.lengthOf(1);
expect(result.data[0].grant_id)
Expand Down Expand Up @@ -756,8 +764,10 @@ describe('db', () => {
expect(result.length).to.equal(0);
});
it('returns a grant whose modification date is one day ago', async () => {
const newGrant = fixtures.grants.healthAide;
newGrant.grant_id = '444816';
const newGrant = {
...fixtures.grants.healthAide,
grant_id: '444816',
};
// Note the use of `Date` -- this ensures compatibility with our mocked time
newGrant.open_date = new Date('2022-06-21');
await knex(TABLES.grants).insert(Object.values([newGrant]));
Expand All @@ -775,15 +785,13 @@ describe('db', () => {
role_id: fixtures.roles.adminRole.id,
agency_id: fixtures.agencies.accountancy.id,
tenant_id: fixtures.tenants.SBA.id,
id: 99991,
},
);
const createdUser = await db.getUser(response.id);
expect(createdUser.emailPreferences.GRANT_ASSIGNMENT).to.equal(emailConstants.emailSubscriptionStatus.subscribed);
expect(createdUser.emailPreferences.GRANT_DIGEST).to.equal(emailConstants.emailSubscriptionStatus.subscribed);
expect(createdUser.emailPreferences.GRANT_FINDER_UPDATES).to.equal(emailConstants.emailSubscriptionStatus.subscribed);
expect(createdUser.emailPreferences.GRANT_INTEREST).to.equal(emailConstants.emailSubscriptionStatus.subscribed);
await db.deleteUser(response.id);
});
});

Expand All @@ -796,13 +804,11 @@ describe('db', () => {
role_id: fixtures.roles.adminRole.id,
agency_id: fixtures.agencies.accountancy.id,
tenant_id: fixtures.tenants.SBA.id,
id: 99991,
},
);
const NAME = 'new name';
const updatedUser = await db.updateUser({ id: user.id, name: NAME });
expect(updatedUser.name).to.equal(NAME);
await db.deleteUser(user.id);
});

it('Updates user\'s avatar', async () => {
Expand All @@ -813,13 +819,11 @@ describe('db', () => {
role_id: fixtures.roles.adminRole.id,
agency_id: fixtures.agencies.accountancy.id,
tenant_id: fixtures.tenants.SBA.id,
id: 99991,
},
);
const HEX_COLOR = '#44337A';
const updatedUser = await db.updateUser({ id: user.id, avatar_color: HEX_COLOR });
expect(updatedUser.avatar_color).to.equal(HEX_COLOR);
await db.deleteUser(user.id);
});

it('Updates fields independently', async () => {
Expand All @@ -830,13 +834,11 @@ describe('db', () => {
role_id: fixtures.roles.adminRole.id,
agency_id: fixtures.agencies.accountancy.id,
tenant_id: fixtures.tenants.SBA.id,
id: 99991,
},
);
const NAME = 'new name';
const updatedUser = await db.updateUser({ id: user.id, name: NAME }); // only changing name
expect(updatedUser.avatar_color).to.include('#'); // avatar_color is a hex color starting with #
await db.deleteUser(user.id);
});
});

Expand All @@ -849,7 +851,6 @@ describe('db', () => {
role_id: fixtures.roles.adminRole.id,
agency_id: fixtures.agencies.accountancy.id,
tenant_id: fixtures.tenants.SBA.id,
id: 99991,
},
);
const createdUser = await db.getUser(response.id);
Expand Down
16 changes: 11 additions & 5 deletions packages/server/__tests__/db/seeds/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ module.exports = {
grantsViewed,
};

// Reset Id sequence where seeds supply predefined id
const insertWithReset = async (knex, tableName, records) => {
await knex(tableName).insert(records);
await knex.raw(`SELECT setval('${tableName}_id_seq', (SELECT MAX(id) from "${tableName}"));`);
};

module.exports.seed = async (knex) => {
// https://stackoverflow.com/a/36499676
// await knex.migrate.rollback();
Expand All @@ -561,11 +567,11 @@ module.exports.seed = async (knex) => {
},
);

await knex(TABLES.tenants).insert(Object.values(tenants));
await knex(TABLES.roles).insert(Object.values(roles));
await knex(TABLES.agencies).insert(Object.values(agencies));
await knex(TABLES.tenants).update({ main_agency_id: agencies.accountancy.id }).where('id', 0);
await knex(TABLES.users).insert(Object.values(users));
await insertWithReset(knex, TABLES.tenants, Object.values(tenants));
await insertWithReset(knex, TABLES.roles, Object.values(roles));
await insertWithReset(knex, TABLES.agencies, Object.values(agencies));
await knex(TABLES.tenants).update({ main_agency_id: agencies.accountancy.id }).where('id', 1);
await insertWithReset(knex, TABLES.users, Object.values(users));
await knex(TABLES.keywords).insert(Object.values(keywords));
await knex(TABLES.interested_codes).insert(Object.values(interestedCodes));
await knex(TABLES.eligibility_codes).insert(Object.values(eligibilityCodes));
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function createUser(user) {
(k) => ({ [k]: emailConstants.emailSubscriptionStatus.subscribed }),
),
);
module.exports.setUserEmailSubscriptionPreference(response[0].id, user.agency_id, emailUnsubscribePreference);
await setUserEmailSubscriptionPreference(response[0].id, user.agency_id, emailUnsubscribePreference);

return {
...user,
Expand Down

0 comments on commit 5b98a4a

Please sign in to comment.