Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature testing jest #7

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Binary file added .DS_Store
Binary file not shown.
9 changes: 9 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"env": {
"test": {
"plugins": [
"@babel/plugin-transform-modules-commonjs"
]
}
}
}
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ module.exports = {
"sourceType": "module"
},
"rules": {
}
},
"ignorePatterns": ['dist/', 'node_modules/']
};
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
hint-report/
dist/
dist/
coverage/
1 change: 1 addition & 0 deletions .stylelintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
13 changes: 13 additions & 0 deletions __mocks__/localstorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default class LocalStorage {
constructor() {
this.localStorage = [];
}

getItems() {
return this.localStorage;
}

setItems(value) {
this.localStorage.push(value);
}
}
52 changes: 52 additions & 0 deletions __test__/task-manipulation.mod.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-disable no-undef */
import { TaskManipulation } from "../src/modules/task-manipulation.mod";
import LocalStorage from "../__mocks__/localstorage.js";
import { renderWithEventListeners } from "../src/modules/task.mod";

// Mock local storage
let testTasks = new LocalStorage();
let index = 0;
let task = class {
constructor(desc) {
this.index = index++;
this.description = desc;
this.checked = false;
}
};

for (let i = 0; i < 3; i++) {
testTasks.setItems(new task("Task " + i));
}

beforeAll(() => {
document.body.innerHTML = `<ul id="task-list" class="flex flex-column w-100"></ul>`;
renderWithEventListeners(testTasks.getItems());
});

describe("TaskManipulation", () => {
describe("removeTask", () => {
test("remove a task from the list", () => {
TaskManipulation.removeTask(0, testTasks.getItems());
expect(testTasks.getItems().length).toBe(2);
});

test("removes li element from the list", () => {
renderWithEventListeners(testTasks.getItems());
const list = document.getElementById("task-list").childNodes.length;
expect(list).toBe(2);
});
});

describe("addTask", () => {
test("add a task to the list", () => {
TaskManipulation.addTask("Task New", testTasks.getItems());
expect(testTasks.getItems().length).toBe(3);
});

test("adds li element to the list", () => {
renderWithEventListeners(testTasks.getItems());
const list = document.getElementById("task-list").childNodes.length;
expect(list).toBe(3);
});
});
});
Loading