Repository navigation
Expand file tree
/
Copy pathtodo.spec.js
More file actions
71 lines (62 loc) · 1.87 KB
/
Copy pathtodo.spec.js
File metadata and controls
71 lines (62 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const nock = require("nock");
const got = require("got");
const expect = require("chai").expect;
const { createLocalServer } = require("./local-server");
const host = "https://todo-app-barkend-b18308c4c059.herokuapp.com/todos/";
describe("todo-app-barkend mocked tests", () => {
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});
it("can get todos", async () => {
const todoObject = {
todos: [
{ task: "Two", _id: 9, completed: false },
{ task: "three", _id: 84, completed: false },
],
};
nock(host).get("/").reply(200, todoObject);
const res = await got(host);
expect(res.body).to.eq(JSON.stringify(todoObject));
expect(res.statusCode).to.equal(200);
});
it("can create todos", async () => {
const createdTodo = {
todos: [{ task: "Cook Dinner Today", _id: 9, completed: false }],
};
nock(host).post("/").reply(200, createdTodo);
const res = await got.post(host, { task: "Cook Breakfast" });
expect(res.body).to.eq(JSON.stringify(createdTodo));
expect(res.statusCode).to.equal(200);
});
});
describe("todo-app-barkend - tests without mocks", () => {
let server;
let localHost;
beforeAll((done) => {
server = createLocalServer().listen(0, "127.0.0.1", () => {
localHost = `http://127.0.0.1:${server.address().port}/todos/`;
done();
});
});
afterAll((done) => {
server.close(done);
});
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
});
it("can create todos - enabled backend", async () => {
var options = {
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
task: "Cook Lunch",
}),
};
nock.enableNetConnect(/127\.0\.0\.1/);
const res = await got.post(localHost, options);
expect(JSON.parse(res.body)).to.have.property("task", "Cook Lunch");
});
});