Skip to main content

4 posts tagged with "typescript"

View All Tags

· One min read
Hreniuc Cristian-Alexandru

When you want to test a function that uses a method from an object, you can mock the method and check if it was called with the right parameters. For example, this is how to mock for sendNotification method from firebaseService object:

import { firebaseService } from '../../../firebase/firebase';

let mockSendNotification: jest.Mock;

describe('Tests suit', () => {

test('Test1', async () => {
mockSendNotification = jest.fn();
firebaseService.sendNotification = mockSendNotification;

// call the function that uses firebaseService.sendNotification

expect(mockSendNotification).toHaveBeenCalledWith(
C1.notifications.N1U4S1.contents,
C1.notifications.N1U4S1.subscriptions,
);
});
});

And if you want to check if the method was called multiple times with different parameters, you can use toHaveBeenNthCalledWith:

expect(mockSendNotification).toHaveBeenNthCalledWith(
1,
C1.notifications.N4U1S2E1.contents,
C1.notifications.N4U1S2E1.subscriptions,
);
expect(mockSendNotification).toHaveBeenNthCalledWith(
2,
C1.notifications.N5U1S2E2.contents,
C1.notifications.N5U1S2E2.subscriptions,
);

· One min read
Hreniuc Cristian-Alexandru

When you have two tables and you want to create an inner join without having a relation in the entities, you can use the innerJoin method from the createQueryBuilder method like this:

.innerJoin(
UserCompany,
'user_company',
'user_company.userId = notification_subscription.userId',
)

This works even if you don't have a relation between the two entities.

const notifForCompanyManagers = await dbConnection.manager
.getRepository(NotificationSubscription)
.createQueryBuilder('notification_subscription')
.select('notification_subscription.token')
.addSelect('notification_subscription.userId')
.innerJoin(
UserCompany,
'user_company',
'user_company.userId = notification_subscription.userId',
)
.where(
'(user_company.manager = true or user_company.owner = true) and user_company.companyId = :companyId',
{ companyId: companyId },
)
.getMany();

· One min read
Hreniuc Cristian-Alexandru

I was using composition API and I had an object/store that I wanted to reset on client logout. I found that I can do it like this:

const company = reactive(new Company());

// Used when we re-login or on logout.
export class CompanyUtil {
public static clear() {
//https://stackoverflow.com/a/61509432
// https://github.com/vuejs/core/issues/1081
Object.assign(company, new Company());
}
}

export default company;