Skip to content

Commit

Permalink
Fix pouchEffectsLedger; update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Flaque committed Dec 1, 2017
1 parent a453044 commit 57cf9df
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
node_modules
.DS_Store
*.log
coverage
5 changes: 5 additions & 0 deletions package.json
Expand Up @@ -18,5 +18,10 @@
"devDependencies": {
"documentation": "^5.3.3",
"jest": "^21.2.1"
},
"jest": {
"collectCoverage": true,
"coverageDirectory": "<rootDir>/coverage",
"coveragePathIgnorePatterns": ["/node_modules/", "/templates/", "/dist/"]
}
}
11 changes: 7 additions & 4 deletions src/index.js
Expand Up @@ -159,7 +159,7 @@ const totalOf = (currency, ...ledgers) => {
* @param {Object} state a state object that will be passed to the cost to determine the type
*/
const buy = (item, wallet, state = {}) => {
if (!item.cost) {
if (!item || !item.cost) {
return wallet;
}

Expand Down Expand Up @@ -204,9 +204,12 @@ const addItem = (item, wallet, amount = 1) => {
* @param {Object} state
*/
const pouchEffectsLedger = (items, wallet, state = {}) => {
const ledgers = items.map(item => {
return scale(item.effect(state), maybe(wallet.get(item.type), 0));
});
const ledgers = items
.map(item => {
if (!item.effect) return;
return scale(item.effect(state), maybe(wallet.get(item.type), 0));
})
.filter(n => n);
return add(...ledgers);
};

Expand Down
26 changes: 26 additions & 0 deletions src/test/merchant.test.js
Expand Up @@ -121,6 +121,10 @@ describe("currencies", () => {
expect(() => currencies({ notA: "ledger" })).toThrow();
});

test("currencies returns a list even if nothing is defined", () => {
expect(List.isList(currencies())).toBe(true);
});

test("currencies returns a list", () => {
const result = currencies(Map({ GOLD: "5" }));
expect(List.isList(result)).toBe(true);
Expand All @@ -142,6 +146,10 @@ describe("totalOf", () => {
expect(() => totalOf("HAHAHA", { NOTA: "ledger" })).toThrow();
});

test("totalOf will give you 0 if nothing is defined", () => {
expect(totalOf()).toBe(0);
});

test("totalOf will give you 0 if the currency doesn't exist", () => {
const one = Map({ GOLD: 5 });
const two = Map({ SILVER: 5 });
Expand All @@ -164,6 +172,11 @@ describe("buy", () => {
})
};

test("buy will return the same wallet if nothing is defined", () => {
const wallet = Map({ Foo: 5 });
expect(buy(null, wallet)).toBe(wallet);
});

test("buy will throw if it receives an item with a cost attribute that's not a function", () => {
expect(() => buy({ cost: "ya", type: "ha" })).toThrow();
});
Expand Down Expand Up @@ -201,6 +214,19 @@ describe("pouchEffectsLedger", () => {
expect(pouchEffectsLedger([], Map({})).size).toBe(0);
});

test("pouchEffectsLedger returns an empty ledger if there are no items with 'effects'", () => {
const MagicSword = {
type: "MagicSword"
};

const ledger = pouchEffectsLedger(
[MagicSword],
Map({ MagicSword: 2, Charm: 1 })
);

expect(ledger.size).toBe(0);
});

test("pouchEffectsLedger returns a ledger with a combination of effects", () => {
const MagicSword = {
type: "MagicSword",
Expand Down

0 comments on commit 57cf9df

Please sign in to comment.