-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.test.js
120 lines (106 loc) · 4.66 KB
/
Table.test.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const Table = require("./Table");
const mock = require("mock-fs");
const TableDoesNotExistError = require("./errors/TableDoesNotExistError");
const fs = require("fs");
//npm i -- save-dev mock-fs
describe("#readData", () => {
describe("With nonexisting table name", () => {
beforeEach(() => mock({ data: {} }));
afterEach(mock.restore);
test("It throws TableDoesNotExistError", async () => {
const table = new Table("table");
await expect(table.readData()).rejects.toThrow(TableDoesNotExistError);
});
});
describe("With an existing table name", () => {
const data = [
{ a: 1, b: 2 },
{ a: 5, b: 4 },
];
beforeEach(() => mock({ data: { "table.json": JSON.stringify(data) } }));
afterEach(mock.restore);
test("It gets all the data in the table", async () => {
const table = new Table("table");
// order doesnot matter
expect(await table.readData()).toEqual(expect.arrayContaining(data));
});
});
});
describe("#insertData", () => {
describe("With nonexisting table name", () => {
beforeEach(() => mock({ data: {} }));
afterEach(mock.restore);
test("It creates a table and inserts record", async () => {
const table = new Table("table");
const data = [
{ a: 1, b: 2 },
{ a: 5, b: 4 },
];
const { _id, ...newRecordAttributes } = await table.insertRecord(data);
expect(JSON.parse(fs.readFileSync(`data/table.json`))).toEqual(
expect.arrayContaining([{ _id, ...newRecordAttributes }])
);
expect(_id).toBeDefined();
expect(Object.values(newRecordAttributes)).toEqual(data);
});
});
describe("With existing table name", () => {
const data = [
{ a: 1, b: 2 },
{ a: 5, b: 4 },
];
beforeEach(() => mock({ data: { "table.json": JSON.stringify(data) } }));
afterEach(mock.restore);
test("It inserts record", async () => {
const table = new Table("table");
const dataToInsert = { a: 8, b: 9 };
const { _id, ...newRecordAttributes } = await table.insertRecord(dataToInsert);
expect(JSON.parse(fs.readFileSync(`data/table.json`))).toEqual(
expect.arrayContaining([...data, { _id, ...newRecordAttributes }])
);
expect(_id).toBeDefined();
expect(newRecordAttributes).toEqual(dataToInsert);
});
});
});
describe("#insertData", () => {
// Test case for inserting record into a nonexisting table
describe("With nonexisting table name", () => {
beforeEach(() => mock({ data: {} })); // Set up a mock environment with an empty data directory
afterEach(mock.restore); // Restore the original environment after each test case
test("It adds the record", async () => {
const table = new Table("table"); // Create a new table instance
const data = [
{ a: 1, b: 2 },
{ a: 5, b: 4 },
]; // Data to be inserted
const { _id, ...newRecordAttributes } = await table.insertRecord(data); // Insert the data and capture the returned record attributes
// Assertion to check if the inserted record is stored in the table JSON file
expect(JSON.parse(fs.readFileSync(`data/table.json`))).toEqual(
expect.arrayContaining([{ _id, ...newRecordAttributes }])
);
expect(_id).toBeDefined(); // Assertion to check if the _id field is defined
expect(Object.values(newRecordAttributes)).toEqual(data); // Assertion to check if the inserted record attributes match the original data
});
});
// Test case for inserting record into an existing table
describe("With existing table name", () => {
const data = [
{ a: 1, b: 2 },
{ a: 5, b: 4 },
]; // Initial data in the table
beforeEach(() => mock({ data: { "table.json": JSON.stringify(data) } })); // Set up a mock environment with a table containing initial data
afterEach(mock.restore); // Restore the original environment after each test case
test("It inserts a record", async () => {
const table = new Table("table"); // Create a new table instance
const dataToInsert = { a: 8, b: 9 }; // Data to be inserted
const { _id, ...newRecordAttributes } = await table.insertRecord(dataToInsert); // Insert the data and capture the returned record attributes
// Assertion to check if the inserted record is stored in the table JSON file
expect(JSON.parse(fs.readFileSync(`data/table.json`))).toEqual(
expect.arrayContaining([...data, { _id, ...newRecordAttributes }])
);
expect(_id).toBeDefined(); // Assertion to check if the _id field is defined
expect(newRecordAttributes).toEqual(dataToInsert); // Assertion to check if the inserted record attributes match the inserted data
});
});
});