Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should get a cat back', async () => {
expect(controller.getByName('Ventus')).resolves.toEqual({
name: 'Ventus',
breed: testBreed1,
age: 4,
});
// using the really cool @golevelup/nestjs-testing module's utility function here
// otherwise we need to pass `as any` or we need to mock all 54+ attributes of Document
const aquaMock = createMock({
name: 'Aqua',
breed: 'Maine Coon',
age: 5,
});
const getByNameSpy = jest
.spyOn(service, 'getOneByName')
.mockResolvedValueOnce(aquaMock);
const getResponse = await controller.getByName('Aqua');
expect(getResponse).toEqual(aquaMock);
expect(getByNameSpy).toBeCalledWith('Aqua');
});
});
it('should update a cat successfully', async () => {
jest.spyOn(model, 'update').mockResolvedValueOnce(true);
jest.spyOn(model, 'findOne').mockReturnValueOnce(
createMock>({
exec: jest.fn().mockResolvedValueOnce({
_id: lasagna,
name: 'Garfield',
breed: 'Tabby',
age: 42,
}),
}),
);
const updatedCat = await service.updateOne({
_id: lasagna,
name: 'Garfield',
breed: 'Tabby',
age: 42,
});
expect(updatedCat).toEqual(mockCat('Garfield', lasagna, 42, 'Tabby'));
});
describe('CatService using createMock without DI', () => {
let service: CatService;
const repo = createMock>();
beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [
CatService,
{
provide: getRepositoryToken(Cat),
useValue: repo,
},
],
}).compile();
service = module.get(CatService);
});
it('should have the repo mocked', async () => {
it('should return true with auth', () => {
const context = createMock();
context.switchToHttp().getRequest.mockReturnValue({
headers: {
authorization: 'auth',
},
});
expect(guard.canActivate(context)).toBeTruthy();
});
it('should return false without auth', () => {
it('should return true for canActivate', async () => {
expect(
await guard.canActivate(createMock()),
).toBeTruthy();
});
});
it('should successfully insert the user', (done) => {
service
.createNewGoogleUser(
createMock({
id: 'googleId',
name: {
familyName: 'test',
givenName: 'test',
},
emails: [{ value: 'test@test.email', verified: true }],
}),
)
.subscribe({
next: (val) => {
expect(val).toEqual(googleUser);
},
error: (err) => {
throw err;
},
complete: complete(insertSpy, done),
it('should not find, but still create ad return a Google User', (done) => {
jest
.spyOn(googleService, 'getByGoogleId')
.mockReturnValueOnce(of(undefined));
const createSpy = jest
.spyOn(googleService, 'createNewGoogleUser')
.mockReturnValueOnce(of(mockGoogleUser));
service
.findOrCreateGoogleUser(createMock())
.subscribe(googleSubscriber(done, createSpy, 1));
});
});
it('should return that the user is authenticated', () => {
expect(
guard.canActivate(
createMock({
switchToHttp: jest.fn().mockReturnValue({
getRequest: jest.fn().mockReturnValue({
isAuthenticated: jest.fn().mockReturnValue(true),
}),
}),
}),
),
).toBe(true);
});
it('should return the user is **not** authenticate', () => {
GqlExecutionContext.create = jest.fn((context: ExecutionContext) => {
return createMock({
getContext: () => ({
req: {
body: {
variables: {
email: testEmail,
},
somethingElse: 'some other value',
},
},
}),
});
});
expect(guard.getRequest(createMock())).toEqual({
body: {
variables: {
email: testEmail,
},
somethingElse: 'some other value',
email: testEmail,
},
});
});
it('should return true for `canActivate`', async () => {
beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [
UserService,
{
provide: DatabaseService,
useValue: {
query: jest.fn(),
insert: jest.fn(),
update: jest.fn(),
updateMany: jest.fn(),
},
},
{
provide: OgmaService,
useValue: createMock(),
},
],
}).compile();
service = module.get(UserService);
db = module.get>(DatabaseService);
});