Files
habitica/test/api/unit/libs/taskManager.js
Matteo Pagliazzi 8f5a0cfe79 Onboarding guide and initial achievements refactoring (#11536)
* add achievements to user

* add placeholder strings

* add to achievements to common script

* add onboarding achievements category

* add notifications

* more notifications

* award achievements

* wip notification panel

* add achievements icons and copy

* do not count onboarding tasks for the created task achievement

* add notes

* sprites, fixes and completion status and reward

* add onboarding panel

* add toggle

* fix toggle size

* fix tests

* fix typo

* add notification

* start adding modal

* fix remove button positionin, timeout, progress bar

* modal + fixes

* disable broken social links from level up modal

* change toggle icon color on hover

* add border bottom to onboarding guide panel

* add collapse animation

* expanded onboarding on first open

* onboarding: flip toggle colors

* onboarding: show progress bar all the time

* onboarding: fix panel closing on click

* onboarding modal: add close icon and fix padding

* wip: add migration for existing users

* fix titles in guide

* fix achievements copy

* do not award completed task achievement when direction is down

* start implementing new achievements

* start migrating client

* remove social links from achievements modals

* prevent skipping tutorial + fix achievement notification

* sync fixes

* start redesign achievement modal

* misc fixes to achievements, polish generic achievement modal and hatched pet modal

* add special badge for onboarding

* fix badge condition

* modals fixes

* hatched pet modal: add close icon

* fix badge typo

* fix justin button

* new scrolling behavior for dropdowns

* fix strings capitalization

* add common tests

* add api unit tests

* add date check

* achievements modal polishing

* typos

* add toggle for achievements categories

* typo

* fix test

* fix edit avatar modal cannot be closed

* finish migration and correct launch date

* fix migration

* migration fixes

* fix tests
2019-12-16 17:20:47 +01:00

237 lines
6.3 KiB
JavaScript

import {
createTasks,
getTasks,
syncableAttrs,
moveTask,
} from '../../../../website/server/libs/taskManager';
import i18n from '../../../../website/common/script/i18n';
import shared from '../../../../website/common/script';
import {
generateUser,
generateGroup,
generateChallenge,
} from '../../../helpers/api-unit.helper';
describe('taskManager', () => {
let user; let group; let
challenge;
const testHabit = {
text: 'test habit',
type: 'habit',
up: false,
down: true,
notes: 1976,
};
let req = {};
let res = {};
beforeEach(() => {
req = {};
res = {};
user = generateUser();
group = generateGroup({
name: 'test group',
type: 'guild',
privacy: 'public',
leader: user._id,
});
challenge = generateChallenge({
name: 'test challenge',
shortName: 'testc',
group: group._id,
leader: user._id,
});
});
it('creates user tasks', async () => {
req.body = testHabit;
res.t = i18n.t;
const newTasks = await createTasks(req, res, { user });
const newTask = newTasks[0];
expect(newTask.text).to.equal(testHabit.text);
expect(newTask.type).to.equal(testHabit.type);
expect(newTask.up).to.equal(testHabit.up);
expect(newTask.down).to.equal(testHabit.down);
expect(newTask.createdAt).to.exist;
});
describe('onboarding', () => {
beforeEach(() => {
user.addAchievement = sinon.spy();
sinon.stub(shared.onboarding, 'checkOnboardingStatus');
});
afterEach(() => {
shared.onboarding.checkOnboardingStatus.restore();
});
it('adds the onboarding achievement to the user and checks the onboarding status', async () => {
req.body = testHabit;
res.t = i18n.t;
user.flags.welcomed = true;
await createTasks(req, res, { user });
expect(user.addAchievement).to.be.calledOnce;
expect(user.addAchievement).to.be.calledWith('createdTask');
expect(shared.onboarding.checkOnboardingStatus).to.be.calledOnce;
expect(shared.onboarding.checkOnboardingStatus).to.be.calledWith(user);
});
it('does not add the onboarding achievement to the user if flags.welcomed is false', async () => {
req.body = testHabit;
res.t = i18n.t;
user.flags.welcomed = false;
await createTasks(req, res, { user });
expect(user.addAchievement).to.not.be.called;
});
it('does not add the onboarding achievement to the user if it\'s already been awarded', async () => {
req.body = testHabit;
res.t = i18n.t;
user.achievements.createdTask = true;
await createTasks(req, res, { user });
expect(user.addAchievement).to.not.be.called;
});
});
it('gets user tasks', async () => {
req.body = testHabit;
res.t = i18n.t;
await createTasks(req, res, { user });
req.body = {};
req.query = {
type: 'habits',
};
const tasks = await getTasks(req, res, { user });
const task = tasks[0];
expect(task.text).to.equal(testHabit.text);
expect(task.type).to.equal(testHabit.type);
expect(task.up).to.equal(testHabit.up);
expect(task.down).to.equal(testHabit.down);
expect(task.createdAt).to.exist;
});
it('creates group tasks', async () => {
req.body = testHabit;
res.t = i18n.t;
const newTasks = await createTasks(req, res, { user, group });
const newTask = newTasks[0];
expect(newTask.text).to.equal(testHabit.text);
expect(newTask.type).to.equal(testHabit.type);
expect(newTask.up).to.equal(testHabit.up);
expect(newTask.down).to.equal(testHabit.down);
expect(newTask.createdAt).to.exist;
expect(newTask.group.id).to.equal(group._id);
});
it('gets group tasks', async () => {
req.body = testHabit;
res.t = i18n.t;
await createTasks(req, res, { user, group });
req.body = {};
req.query = {
type: 'habits',
};
const tasks = await getTasks(req, res, { user, group });
const task = tasks[0];
expect(task.text).to.equal(testHabit.text);
expect(task.type).to.equal(testHabit.type);
expect(task.up).to.equal(testHabit.up);
expect(task.down).to.equal(testHabit.down);
expect(task.createdAt).to.exist;
expect(task.group.id).to.equal(group._id);
});
it('creates challenge tasks', async () => {
req.body = testHabit;
res.t = i18n.t;
const newTasks = await createTasks(req, res, { user, challenge });
const newTask = newTasks[0];
expect(newTask.text).to.equal(testHabit.text);
expect(newTask.type).to.equal(testHabit.type);
expect(newTask.up).to.equal(testHabit.up);
expect(newTask.down).to.equal(testHabit.down);
expect(newTask.createdAt).to.exist;
expect(newTask.challenge.id).to.equal(challenge._id);
});
it('gets challenge tasks', async () => {
req.body = testHabit;
res.t = i18n.t;
await createTasks(req, res, { user, challenge });
req.body = {};
req.query = {
type: 'habits',
};
const tasks = await getTasks(req, res, { user, challenge });
const task = tasks[0];
expect(task.text).to.equal(testHabit.text);
expect(task.type).to.equal(testHabit.type);
expect(task.up).to.equal(testHabit.up);
expect(task.down).to.equal(testHabit.down);
expect(task.createdAt).to.exist;
expect(task.challenge.id).to.equal(challenge._id);
});
it('returns syncable attibutes', async () => {
req.body = testHabit;
res.t = i18n.t;
const tasks = await createTasks(req, res, { user, challenge });
const syncableTask = syncableAttrs(tasks[0]);
expect(syncableTask._id).to.not.exist;
expect(syncableTask.userId).to.not.exist;
expect(syncableTask.challenge).to.not.exist;
expect(syncableTask.history).to.not.exist;
expect(syncableTask.tags).to.not.exist;
expect(syncableTask.completed).to.not.exist;
expect(syncableTask.streak).to.not.exist;
expect(syncableTask.notes).to.not.exist;
expect(syncableTask.updatedAt).to.not.exist;
});
it('moves tasks to a specified position', async () => {
const order = ['task-id-1', 'task-id-2'];
moveTask(order, 'task-id-2', 0);
expect(order).to.eql(['task-id-2', 'task-id-1']);
});
it('moves tasks to a specified position out of length', async () => {
const order = ['task-id-1'];
moveTask(order, 'task-id-2', 2);
expect(order).to.eql(['task-id-1', 'task-id-2']);
});
});