Created
November 4, 2018 20:46
-
-
Save VivekNayyar/6d02f2cd4a2e1d41ed2a0c7fcfe939a6 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
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