Skip to content

Commit

Permalink
Added more unit test to increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubham Sharma committed Mar 21, 2024
1 parent 325c4bd commit bd296b9
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 22 deletions.
48 changes: 26 additions & 22 deletions app/controllers/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ export default class TasksController extends Controller {
if (taskData.status || taskData.percentCompleted) {
try {
const response = await fetch(
`${API_BASE_URL}/tasks/self/${taskId}?userStatusFlag=true`,
`${API_BASE_URL}/tasks/self/${taskId}${
this.dev ? '?userStatusFlag=true' : ''
}`,
{
method: 'PATCH',
body: JSON.stringify(cleanBody),
Expand Down Expand Up @@ -281,33 +283,33 @@ export default class TasksController extends Controller {
this.showModal = true;
this.buttonRequired = true;
}
isTaskStatusChanged(taskStatus, task) {
isTaskStatusChanged(task, taskStatus) {
return task.status === taskStatus;
}
isProgressChanged(progress, task) {
parseInt(task.percentCompleted || 0) === progress;
isProgressChanged(task, progress) {
return parseInt(task.percentCompleted || 0) === progress;
}
shouldTaskProgressBe100(taskId) {
const currentTask = this.getTaskById(taskId);
const taskData = this.taskFields;
const isCurrentTaskStatusBlock = this.isTaskStatusChanged(
this.TASK_KEYS.BLOCKED,
currentTask
currentTask,
this.TASK_KEYS.BLOCKED
);
const isCurrentTaskStatusInProgress = this.isTaskStatusChanged(
this.TASK_KEYS.IN_PROGRESS,
currentTask
currentTask,
this.TASK_KEYS.IN_PROGRESS
);
const isNewStatusInProgress = this.isTaskStatusChanged(
this.TASK_KEYS.IN_PROGRESS,
taskData
taskData,
this.TASK_KEYS.IN_PROGRESS
);
const isNewTaskStatusBlock = this.isTaskStatusChanged(
this.TASK_KEYS.BLOCKED,
taskData
taskData,
this.TASK_KEYS.BLOCKED
);

const isCurrProgress100 = this.isProgressChanged(100, currentTask);
const isCurrProgress100 = this.isProgressChanged(currentTask, 100);
return (
(isCurrentTaskStatusBlock || isCurrentTaskStatusInProgress) &&
!isNewStatusInProgress &&
Expand All @@ -319,14 +321,14 @@ export default class TasksController extends Controller {
const taskData = this.taskFields;
const currentTask = this.getTaskById(taskId);
const isCurrentTaskStatusBlock = this.isTaskStatusChanged(
this.TASK_KEYS.BLOCKED,
currentTask
currentTask,
this.TASK_KEYS.BLOCKED
);
const isNewStatusInProgress = this.isTaskStatusChanged(
this.TASK_KEYS.IN_PROGRESS,
taskData
taskData,
this.TASK_KEYS.IN_PROGRESS
);
const isCurrProgress0 = this.isProgressChanged(0, currentTask);
const isCurrProgress0 = this.isProgressChanged(currentTask, 0);
return (
isNewStatusInProgress && !isCurrentTaskStatusBlock && !isCurrProgress0
);
Expand All @@ -351,10 +353,12 @@ export default class TasksController extends Controller {
this.resetCurrentTask = resetCurrentTask;
const taskData = this.taskFields;
this.tempTaskId = taskId;
const msg = this.getInfoMsg(taskId);
if (this.dev && taskData.status && msg) {
this.showTaskChangeInfoModal(msg);
return;
if (this.dev && taskData.status) {
const msg = this.getInfoMsg(taskId);
if (msg) {
this.showTaskChangeInfoModal(msg);
return;
}
}

if (
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/components/tasks-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ module('Integration | Component | tasks', function (hooks) {
ctrl.message,
'The progress of current task is 25%. Proceeding further will make task progress 0%.'
);
assert.equal(ctrl.taskFields.percentCompleted, 0);
});
test('changing the task status from IN_PROGRESS to any status other than BLOCKED inform proceeding further will make progress 100', async function (assert) {
tasks[0].status = 'IN_PROGRESS';
Expand All @@ -129,6 +130,7 @@ module('Integration | Component | tasks', function (hooks) {
ctrl.message,
'The progress of current task is 25%. Proceeding further will make task progress 100%.'
);
assert.equal(ctrl.taskFields.percentCompleted, 100);
});
test('changing the task status from BLOCKED to any status other than IN_PROGRESS when percentCompleted is less than 100, inform the user proceeding further will make progress 100', async function (assert) {
tasks[0].status = 'BLOCKED';
Expand All @@ -154,5 +156,29 @@ module('Integration | Component | tasks', function (hooks) {
ctrl.message,
`The progress of current task is ${tasks[0].percentCompleted}%. Proceeding further will make task progress 100%.`
);
assert.equal(ctrl.taskFields.percentCompleted, 100);
});
test('changing the task status from any status other than blocked and in progress to other status does not result in showing modal', async function (assert) {
tasks[0].status = 'SMOKE_TESTING';
this.set('dev', true);
const ctrl = this.owner.lookup('controller:tasks');

// before action
assert.equal(ctrl.showModal, false);
assert.equal(ctrl.message, '');

ctrl.allTasks = tasks;
ctrl.dev = true;
ctrl.taskFields = {
status: 'NEEDS_REVIEW',
};

// action
ctrl.send('handleUpdateTask', tasks[0].id, () => {});

// after action
assert.equal(ctrl.showModal, false);
assert.equal(ctrl.message, '');
assert.equal(ctrl.taskFields.percentCompleted, undefined);
});
});
125 changes: 125 additions & 0 deletions tests/unit/components/tasks-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { tasks } from 'website-my/tests/fixtures/tasks';
import { TASK_KEYS, TASK_MESSAGES } from 'website-my/constants/tasks';

module('Unit | Component | tasks', function (hooks) {
setupRenderingTest(hooks);
test('shouldTaskProgressBe100 function should return true if task is moving from in progress to task status other than blocked', async function (assert) {
tasks[0].status = 'IN_PROGRESS';
const ctrl = this.owner.lookup('controller:tasks');

ctrl.getTaskById = () => tasks[0];
ctrl.taskFields = {
status: 'SMOKE_TESTING',
};

const res = ctrl.shouldTaskProgressBe100(tasks[0].id);

assert.equal(res, true);
});
test('shouldTaskProgressBe100 function should return false if task is moving from in progress to blocked', async function (assert) {
tasks[0].status = 'BLOCKED';
const ctrl = this.owner.lookup('controller:tasks');

ctrl.getTaskById = () => tasks[0];
ctrl.taskFields = {
status: 'IN_PROGRESS',
};

const res = ctrl.shouldTaskProgressBe100(tasks[0].id);

assert.equal(res, false);
});
test('shouldTaskProgressBe0 function should return true if task is moving to in progress from any status other than block', async function (assert) {
tasks[0].status = 'SMOKE_TESTING';
const ctrl = this.owner.lookup('controller:tasks');

ctrl.getTaskById = () => tasks[0];

ctrl.taskFields = {
status: 'IN_PROGRESS',
};

const res = ctrl.shouldTaskProgressBe0(tasks[0].id);

assert.equal(res, true);
});
test('shouldTaskProgressBe0 function should return false if task is moving to in progress from blocked status', async function (assert) {
tasks[0].status = 'BLOCKED';
const ctrl = this.owner.lookup('controller:tasks');

ctrl.getTaskById = () => tasks[0];

ctrl.taskFields = {
status: 'IN_PROGRESS',
};

const res = ctrl.shouldTaskProgressBe0(tasks[0].id);

assert.equal(res, false);
});
test('getInfoMsg function should return current progress and warning that proceeding further will make progress 100, if shouldTaskProgressBe100 returns true', async function (assert) {
tasks[0].status = 'SMOKE_TESTING';
const ctrl = this.owner.lookup('controller:tasks');

ctrl.getTaskById = () => tasks[0];

ctrl.shouldTaskProgressBe100 = () => true;

const res = ctrl.getInfoMsg(tasks[0].id);
const msg = `The progress of current task is ${tasks[0].percentCompleted}%. ${TASK_MESSAGES.CHANGE_TO_100_PROGRESS}`;

assert.equal(res, msg);
});
test('getInfoMsg function should return current progress and warning that proceeding further will make progress 0, if shouldTaskProgressBe0 returns true', async function (assert) {
const ctrl = this.owner.lookup('controller:tasks');

ctrl.getTaskById = () => tasks[0];
ctrl.shouldTaskProgressBe0 = () => true;

const res = ctrl.getInfoMsg(tasks[0].id);
const msg = `The progress of current task is ${tasks[0].percentCompleted}%. ${TASK_MESSAGES.CHANGE_TO_0_PROGRESS}`;

assert.equal(res, msg);
});
test('getInfoMsg function should return undefined, if both shouldTaskProgressBe0 and shouldTaskProgressBe100 returns false', async function (assert) {
const ctrl = this.owner.lookup('controller:tasks');

ctrl.getTaskById = () => tasks[0];
ctrl.shouldTaskProgressBe0 = () => false;
ctrl.shouldTaskProgressBe100 = () => false;

const res = ctrl.getInfoMsg(tasks[0].id);

assert.equal(res, undefined);
});
test('isProgressChanged function should return true if percentCompleted of task is same as passed down', async function (assert) {
const ctrl = this.owner.lookup('controller:tasks');

const res = ctrl.isProgressChanged(tasks[0], 25);

assert.equal(res, true);
});
test('isProgressChanged function should return false if percentCompleted of task is different then passed down value', async function (assert) {
const ctrl = this.owner.lookup('controller:tasks');

const res = ctrl.isProgressChanged(tasks[0], 5);

assert.equal(res, false);
});
test('isTaskStatusChanged function should return true if status of task is same as passed down value', async function (assert) {
const ctrl = this.owner.lookup('controller:tasks');

const res = ctrl.isTaskStatusChanged(tasks[0], tasks[0].status);

assert.equal(res, true);
});
test('isTaskStatusChanged function should return false if status of task is different then passed down value', async function (assert) {
const ctrl = this.owner.lookup('controller:tasks');

const res = ctrl.isTaskStatusChanged(tasks[0], TASK_KEYS.SANITY_CHECK);

assert.equal(res, false);
});
});

0 comments on commit bd296b9

Please sign in to comment.