Skip to content

Instantly share code, notes, and snippets.

@gonzalolarrosa
Created August 1, 2018 19:41
Show Gist options
  • Save gonzalolarrosa/6e7e1d80ecb9727a561c4db2d1a10936 to your computer and use it in GitHub Desktop.
Save gonzalolarrosa/6e7e1d80ecb9727a561c4db2d1a10936 to your computer and use it in GitHub Desktop.
import { getUserData } from '../Service';
const USER_ID = 1;
const USER_INFO = {
name: ‘Bruce Wayne’,
age: 32,
gender: ‘male’,
mail: ‘batman@dccomics.com’
}
describe("Callbacks", () => {
it("should return the user personal data", (done) => {
function callback(userInfo) = {
expect(userInfo).toBe(USER_INFO);
done();
}
getUserData(callback);
});
});
describe("Promises", () => {
it("should return the user personal data", () => {
return getUserData(USER_ID)
.then(userInfo => {
expect(userInfo).toBe(USER_INFO);
});
});
it("should return error if USER_ID is not provided", () => {
return getUserData()
.catch(error => expect(error).toMatch(‘error’));
});
});
describe("Async/Await", () => {
it("should return the user personal data", async () => {
const userInfo = await getUserData(USER_ID);
expect(userInfo).toBe(USER_INFO);
});
it("should return error if USER_ID is not provided", async () => {
try {
await getUserData();
} catch(error) {
expect(error).toMatch(‘error’);
}
});
});
describe("Mix", () => {
it("should return the user personal data", (done) => {
getUserData(USER_ID)
.then(({userInfo}) => {
expect(userInfo).toEqual(USER_INFO);
done();
});
});
it("should return error if USER_ID is not provided", (done) => {
getUserData()
.catch(error => {
expect(error).toMatch(‘error’);
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment