Skip to content

Instantly share code, notes, and snippets.

@VivekNayyar
Created November 4, 2018 20:46
Show Gist options
  • Save VivekNayyar/6d02f2cd4a2e1d41ed2a0c7fcfe939a6 to your computer and use it in GitHub Desktop.
Save VivekNayyar/6d02f2cd4a2e1d41ed2a0c7fcfe939a6 to your computer and use it in GitHub Desktop.
export function fullName(firstName, lastName, callback) {
callback(`${firstName} ${lastName}`);
}
export function promiseExample() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(true);
}, 100);
});
}
/* Tests */
test('callback funct gets called', () => {
const mockedCallback = jest.fn();
fullName('Trusting', 'Social', mockedCallback);
expect(mockedCallback).toHaveBeenCalled();
});
test('callback funct mock implementation', () => {
const mockedCallback = jest.fn();
mockedCallback.mockImplementation((fullName) => console.log(`Hello ${fullName}`);
fullName('Trusting', 'Social', mockedCallback);
expect(mockedCallback).toHaveBeenCalledWith('Trusting Social');
});
test('mock implementation with return value', () => {
const mockedCallback = jest.fn();
mockedCallback.mockReturnValue('Hello Mocked Return Value');
expect(fullName('Trusting', 'Social')).toBe('Hello Mocked Return Value');
expect(mockedCallback).toHaveBeenCalledWith('Trusting Social');
});
test('async await with jest', async () => {
const promiseResolvedValue = await promiseExample();
expect(promiseResolvedValue).toBeTruthy();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment