Skip to content

Commit

Permalink
비동기 처리 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
Codiving committed Mar 2, 2022
1 parent aa6c33a commit c51bbe7
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/AsyncTest/asynctest.js
@@ -0,0 +1,29 @@
// callback 비동기 처리
const LoginFns = {
cbLogin: (cb, result) => {
setTimeout(() => {
if (!result) {
cb(() => {
throw new Error("network 에러");
});
} else {
cb(() => "로그인 성공");
}
}, 1000);
},

// promise 비동기 처리 & async await 비동기 처리
promiseLogin: result => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (!result)
reject(() => {
throw new Error("network 에러");
});
else resolve("로그인 성공");
}, 1000);
});
}
};

module.exports = LoginFns;
97 changes: 97 additions & 0 deletions src/AsyncTest/test/asynctest.test.js
@@ -0,0 +1,97 @@
const { cbLogin, promiseLogin } = require("../asynctest.js");

describe("비동기 처리 테스트", () => {
// callback;
describe("callback 방식", () => {
it("1초 뒤 로그인 성공", done => {
function loginTest(result) {
try {
expect(result()).toBe("로그인 성공");
done();
} catch (error) {
expect(() => result()).toThrow("network 에러");
done();
}
}
cbLogin(loginTest, true);
});

it("1초 뒤 로그인 실패", done => {
function loginTest(result) {
try {
expect(result()).toBe("로그인 성공");
done();
} catch (error) {
expect(() => result()).toThrow("network 에러");
done();
}
}
cbLogin(loginTest, false);
});
});

// promise;
describe("promise 방식", () => {
it("done - 1초 뒤 로그인 성공", done => {
promiseLogin(true).then(result => {
expect(result).toBe("로그인 성공");
done();
});
});

it("done - 1초 뒤 로그인 실패", done => {
promiseLogin(false)
.then()
.catch(result => {
expect(() => result()).toThrow("network 에러");
done();
});
});

it("return - 1초 뒤 로그인 성공", () => {
return promiseLogin(true).then(result => {
expect(result).toBe("로그인 성공");
});
});

it("return - 1초 뒤 로그인 실패", () => {
return promiseLogin(false)
.then()
.catch(result => {
expect(() => result()).toThrow("network 에러");
});
});

it("resolves - 1초 뒤 로그인 성공", () => {
return expect(promiseLogin(true)).resolves.toBe("로그인 성공");
});

it("rejects - 1초 뒤 로그인 실패", () => {
return expect(promiseLogin(false)).rejects.toThrow("network 에러");
});
});

// async await
describe("async await 방식", () => {
it("async await - 1초 뒤 로그인 성공", async () => {
const result = await promiseLogin(true);
expect(result).toBe("로그인 성공");
});

it("async await - 1초 뒤 로그인 실패", async () => {
try {
return await promiseLogin(false);
} catch (result) {
expect(() => result()).toThrow("network 에러");
}
});

it("async await & resolvers - 1초 뒤 로그인 성공", async () => {
return await expect(promiseLogin(true)).resolves.toBe("로그인 성공");
});

it("async await & rejects - 1초 뒤 로그인 실패", async () => {
return await expect(promiseLogin(false)).rejects.toThrow("network 에러");
});
});
});

0 comments on commit c51bbe7

Please sign in to comment.