Files
habitica/test/common/libs/inAppRewards.js
negue f35ef3a046 Multiple checks for pinnedItems (#11031)
* remove null/undefined entries from pinnedItems when an item is toggled - more inner checks + test

* drawer: fix when there isn't a page available

* rollback cleaning up pinnedEntries on item-toggle

* remove "re-setting" pinnedItems

* remove the filter
2019-03-31 20:41:37 +02:00

97 lines
3.2 KiB
JavaScript

import {
generateUser,
} from '../../helpers/common.helper';
import getOfficialPinnedItems from '../../../website/common/script/libs/getOfficialPinnedItems.js';
import inAppRewards from '../../../website/common/script/libs/inAppRewards';
describe('inAppRewards', () => {
let user;
let officialPinnedItems;
let officialPinnedItemPaths;
let testPinnedItems;
let testPinnedItemsOrder;
beforeEach(() => {
user = generateUser();
officialPinnedItems = getOfficialPinnedItems(user);
officialPinnedItemPaths = [];
// officialPinnedItems are returned in { type: ..., path:... } format but we just need the paths for testPinnedItemsOrder
if (officialPinnedItems.length > 0) {
officialPinnedItemPaths = officialPinnedItems.map(item => item.path);
}
testPinnedItems = [
{ type: 'armoire', path: 'armoire' },
{ type: 'potion', path: 'potion' },
{ type: 'marketGear', path: 'gear.flat.weapon_warrior_1' },
{ type: 'marketGear', path: 'gear.flat.head_warrior_1' },
{ type: 'marketGear', path: 'gear.flat.armor_warrior_1' },
{ type: 'hatchingPotions', path: 'hatchingPotions.Golden' },
{ type: 'marketGear', path: 'gear.flat.shield_warrior_1' },
{ type: 'card', path: 'cardTypes.greeting' },
{ type: 'potion', path: 'hatchingPotions.Golden' },
{ type: 'card', path: 'cardTypes.thankyou' },
{ type: 'food', path: 'food.Saddle' },
];
testPinnedItemsOrder = [
'hatchingPotions.Golden',
'cardTypes.greeting',
'armoire',
'gear.flat.weapon_warrior_1',
'gear.flat.head_warrior_1',
'cardTypes.thankyou',
'gear.flat.armor_warrior_1',
'food.Saddle',
'gear.flat.shield_warrior_1',
'potion',
];
// For this test put seasonal items at the end so they stay out of the way
testPinnedItemsOrder = testPinnedItemsOrder.concat(officialPinnedItemPaths);
});
it('returns the pinned items in the correct order', () => {
user.pinnedItems = testPinnedItems;
user.pinnedItemsOrder = testPinnedItemsOrder;
let result = inAppRewards(user);
expect(result[2].path).to.eql('armoire');
expect(result[9].path).to.eql('potion');
});
it('ignores null/undefined entries', () => {
user.pinnedItems = testPinnedItems;
user.pinnedItems.push(null);
user.pinnedItems.push(undefined);
user.pinnedItemsOrder = testPinnedItemsOrder;
let result = inAppRewards(user);
expect(result[2].path).to.eql('armoire');
expect(result[9].path).to.eql('potion');
});
it('does not return seasonal items which have been unpinned', () => {
if (officialPinnedItems.length === 0) {
return; // if no seasonal items, this test is not applicable
}
let testUnpinnedItem = officialPinnedItems[0];
let testUnpinnedPath = testUnpinnedItem.path;
let testUnpinnedItems = [
{ type: testUnpinnedItem.type, path: testUnpinnedPath},
];
user.pinnedItems = testPinnedItems;
user.pinnedItemsOrder = testPinnedItemsOrder;
user.unpinnedItems = testUnpinnedItems;
let result = inAppRewards(user);
let itemPaths = result.map(item => item.path);
expect(itemPaths).to.not.include(testUnpinnedPath);
});
});