Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
test: improve to 100% coverage :)
Browse files Browse the repository at this point in the history
  • Loading branch information
totorogendut committed Jul 9, 2020
1 parent 8a2084d commit 77edd7c
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/fund/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ export const invalidWeight = (address: string, weight: string | number | undefin
* *
*****************************/
export const noUndefinedFundOnServerSide = "can't use fund() with empty parameters in server side.";
export const invalidFundmeServerSide = "invalid fundme parameters on the server-side.";
export const invalidFundmeServerSide = "invalid fundme on the server-side.";
23 changes: 17 additions & 6 deletions src/fund/fund-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ import { isMultiplePointer, setFundType, getDefaultAddress, defaultAddressMultip
import { setPointerSingle } from "./set-pointer-single";
import { setPointerFromTemplates } from "./set-pointer-template";
import { setPointerMultiple } from "./set-pointer-multiple";
import { defaultAddressNotFound, invalidAddress, FundmeError } from "./errors";
import {
defaultAddressNotFound,
invalidAddress,
FundmeError,
invalidFundmeServerSide,
} from "./errors";
import { FundType } from "./fund";
import { isNode } from "browser-or-node";

export function clientSideFund(pointer?: WMAddress, options: fundOptions = {}): FundType {
if (pointer === undefined) {
export function clientSideFund(pointer?: WMAddress, options?: fundOptions): FundType {
if (pointer === undefined || pointer === null) {
setPointerFromTemplates();
return setFundType(FundType.isFromTemplate);
}

if (options && options.force === "server") {
throw FundmeError(invalidFundmeServerSide);
}

if (typeof pointer === "string") {
if (pointer === "default") {
if (getDefaultAddress() !== undefined) {
Expand All @@ -38,10 +46,13 @@ export function clientSideFund(pointer?: WMAddress, options: fundOptions = {}):
}

let forceBrowser: boolean = false;
export function forceFundmeOnBrowser() {
forceBrowser = true;
export function forceFundmeOnBrowser(force: boolean = true) {
forceBrowser = force;
}

const isNode =
typeof process !== "undefined" && process.versions != null && process.versions.node != null;

export const isBrowser = (options: fundOptions = {}): boolean => {
if (options.force === "server") return false;
const forced = forceBrowser;
Expand Down
3 changes: 2 additions & 1 deletion src/fund/fund-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { isMultiplePointer } from "./utils";
import { setPointerMultiple } from "./set-pointer-multiple";
import { FundmeError, invalidFundmeServerSide, noUndefinedFundOnServerSide } from "./errors";
import { setPointerSingle } from "./set-pointer-single";
import { isNullOrUndefined } from "util";

export function serverSideFund(pointer: WMAddress): string {
if (pointer === undefined) throw FundmeError(noUndefinedFundOnServerSide);
if (pointer === null || pointer === undefined) throw FundmeError(noUndefinedFundOnServerSide);

if (typeof pointer === "string") {
return setPointerSingle(pointer).toString();
Expand Down
2 changes: 1 addition & 1 deletion src/fund/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface fundOptions {
maxPool?: number;
default?: boolean;
affiliateId?: string;
affiliateEntry?: boolean;
isAffiliateEntry?: boolean;
}

interface defaultAddressOptions {
Expand Down
17 changes: 12 additions & 5 deletions src/fund/relative-weight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ export function filterRelativeWeight(pointer: WMPointer): boolean {
return false;
}

throw FundmeError(invalidWeight(pointer.address ?? pointer, weight));
throw FundmeError(invalidWeight(pointer.address, weight));
}

export function registerRelativeWeight(pointer: WMPointer) {
pointer.weight = getWeight(pointer);
pointer.weight = getRelativeWeight(pointer);
relativeWeightPointers.push(pointer);
}

Expand Down Expand Up @@ -105,7 +105,7 @@ export function normalizeRelativePointers(pool: WMPointer[], sum: number): WMPoi
});
}

export function getWeight(pointer: string | WMPointer): number {
export function getRelativeWeight(pointer: string | WMPointer): number {
let chance;

if (typeof pointer === "string") {
Expand All @@ -117,9 +117,16 @@ export function getWeight(pointer: string | WMPointer): number {
}
} else {
if (!pointer.weight) {
throw FundmeError(weightForRelativePointerNotFound(pointer.address ?? pointer));
throw FundmeError(weightForRelativePointerNotFound(pointer.address));
}
if (typeof pointer.weight === "string") pointer.weight = parseFloat(pointer.weight);

if (typeof pointer.weight === "string") {
if (!pointer.weight.endsWith("%")) throw FundmeError(relativeWeightMustEndsWithPercentage);
pointer.weight = parseFloat(pointer.weight);
} else {
throw FundmeError(invalidRelativeWeight(pointer.address));
}

chance = pointer.weight / 100;
}

Expand Down
6 changes: 5 additions & 1 deletion src/fund/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function getPoolWeightSum(pointers: WMPointer[]): number {
}, 0);
}

export function getWinningPointer(pointers: WMPointer[], choice: number): WMPointer | undefined {
export function getWinningPointer(pointers: WMPointer[], choice: number): WMPointer {
for (const pointer in pointers) {
const weight: weight = pointers[pointer].weight ?? DEFAULT_WEIGHT; // TODO - safecheck null assertion

Expand All @@ -69,6 +69,10 @@ export function getWinningPointer(pointers: WMPointer[], choice: number): WMPoin
return pointers[pointer];
}
}

// Decide if this will be the default behavior later
// in case unexpected case where choice is greater than all pointers' weight
return pointers[0];
}

export function hasAddress(obj: any): boolean {
Expand Down
20 changes: 11 additions & 9 deletions tests/fund/client-side/fund.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ describe("correctly fund() argument", () => {
});

test("get multiple pointer if parameter is an array", () => {
forceFundmeOnBrowser();
const myFundingType = fund([
"test",
"address2",
{
address: "Wooooww",
weight: 4,
},
]);
const myFundingType = fund(
[
"test",
"address2",
{
address: "Wooooww",
weight: 4,
},
],
{ force: "client" },
);

expect(myFundingType).toBe("multiple");
});
Expand Down
13 changes: 12 additions & 1 deletion tests/fund/client-side/fundme-force-browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { fund } from "../../../src/fund/mod";
import { isBrowser } from "../../../src/fund/fund-browser";
import { isBrowser, forceFundmeOnBrowser, clientSideFund } from "../../../src/fund/fund-browser";

//@ts-ignore
import { toBeInTheDocument, toHaveAttribute } from "@testing-library/jest-dom/matchers";
import { FundType } from "../../../src/fund/fund";
import { FundmeError, invalidFundmeServerSide } from "../../../src/fund/errors";

expect.extend({ toBeInTheDocument, toHaveAttribute });

Expand Down Expand Up @@ -44,4 +46,13 @@ describe("test isBrowser()", () => {
test("without forcing in node environment", () => {
expect(isBrowser()).toBeFalsy();
});
test("throw error if forcing client and server in conflict", () => {
const pointer = "$wallet.example.com/test1";

expect(() => {
clientSideFund(pointer, {
force: "server",
});
}).toThrowError(FundmeError(invalidFundmeServerSide));
});
});
50 changes: 45 additions & 5 deletions tests/fund/client-side/relative-weight.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
calculateRelativeWeight,
mockVariables,
clear,
getWeight,
getRelativeWeight,
filterRelativeWeight,
normalizeFixedPointers,
normalizeRelativePointers,
Expand Down Expand Up @@ -309,6 +309,7 @@ describe("filtering relative weight payment pointers pool", () => {
},
];
expect(mock.filter(filterRelativeWeight)).toEqual(filteredMock);
expect(filterRelativeWeight({ address: "$wallet.address.com/testing" })).toBeFalsy();
});
});

Expand All @@ -323,6 +324,9 @@ describe("normalize payment pointers", () => {
address: "$wallet.example.com/test-2",
weight: 60,
},
{
address: "$wallet.example.com/test-3",
},
];
const resultMock = [
{
Expand All @@ -333,6 +337,10 @@ describe("normalize payment pointers", () => {
address: "$wallet.example.com/test-2",
weight: 45,
},
{
address: "$wallet.example.com/test-3",
weight: 3.75,
},
];
expect(normalizeFixedPointers(mock, 0.25)).toEqual(resultMock);
});
Expand All @@ -344,24 +352,56 @@ describe("normalize payment pointers", () => {
});

describe("relative weight getWeight() function", () => {
const mockVariableTotalWeight = 55;
test("basic relative getWeight()", () => {
const mockVariableTotalWeight = 55;
const percentWeight = 10;
const pointer = `$wallet.example.com/test#${percentWeight}%`;
mockVariables();
expect(getWeight(pointer)).toBe(mockVariableTotalWeight / percentWeight);
expect(getRelativeWeight(pointer)).toBe(mockVariableTotalWeight / percentWeight);
});

test("getWeight() with object pointer", () => {
const pointer = {
address: "$wallet.example.com/testing",
weight: "10%",
};
mockVariables();
expect(getRelativeWeight(pointer)).toBe(5.5);
});

test("throw if relative weight not end with %", () => {
const pointer = "$wallet.example.com/test#11";
expect(() => getWeight(pointer)).toThrowError(
expect(() => getRelativeWeight(pointer)).toThrowError(
FundmeError(relativeWeightMustEndsWithPercentage),
);
});
test("throw if relative weight not end with %", () => {
const pointer = {
address: "$wallet.example.com/test",
weight: "11",
};
expect(() => getRelativeWeight(pointer)).toThrowError(
FundmeError(relativeWeightMustEndsWithPercentage),
);
});

test("throw if getWeight() recieves no weight pointer", () => {
const pointer = { address: "$wallet.example.com/test" };
expect(() => getRelativeWeight(pointer)).toThrowError(
FundmeError(weightForRelativePointerNotFound(pointer.address)),
);
});

test("throw if getWeight() recieves invalid weight", () => {
const pointer = { address: "$wallet.example.com/test", weight: true };
expect(() => getRelativeWeight(pointer)).toThrowError(
FundmeError(invalidRelativeWeight(pointer.address)),
);
});

test("throw error if no weight found", () => {
const pointer = { address: "$wallet.address.com/test" };
expect(() => getWeight(pointer)).toThrowError(
expect(() => getRelativeWeight(pointer)).toThrowError(
FundmeError(weightForRelativePointerNotFound(pointer.address)),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("parsing custom syntax", () => {
</template>
`;
forceFundmeOnBrowser();
fund();
fund(null);
const expectedPool = [
{
address: "$wallet.example.com/testing-one",
Expand Down
61 changes: 54 additions & 7 deletions tests/fund/client-side/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import {
getPoolWeightSum,
getWinningPointer,
isNumberOnly,
convertToPointerPool,
} from "../../../src/fund/utils";
import { getCurrentPointerAddress } from "../../../src/fund/mod";
import { forceFundmeOnBrowser } from "../../../src/fund/fund-browser";
import { metaTagNotFound, FundmeError } from "../../../src/fund/errors";
import {
metaTagNotFound,
FundmeError,
getWinningPointerMustBeANumber,
} from "../../../src/fund/errors";

//@ts-ignore
import { toBeInTheDocument, toHaveAttribute } from "@testing-library/jest-dom/matchers";
Expand Down Expand Up @@ -44,25 +49,61 @@ describe("check multiple pointers correctly", () => {
describe("ensure pickPointer() is robust", () => {
const myPointers = [
{
address: "coolguy",
weight: 33,
address: "$wallet.example.com/first-pointer",
weight: 48,
},
{
address: "someother guy",
address: "$wallet.example.com/winning-pointer",
weight: 22,
},
{
address: "is doesn't matter",
address: "$wallet.example.com/not-winning-pointer",
weight: 45,
},
{
address: "$wallet.example.com/pointer-with-default-weight",
},
];
const choice = 50;
test("get correct sum for pool weight", () => {
expect(getPoolWeightSum(myPointers)).toBe(100);
expect(getPoolWeightSum(myPointers)).toBe(120);
});

test("pick correct winning pointer from pool", () => {
expect(getWinningPointer(myPointers, choice).address).toBe("someother guy");
expect(getWinningPointer(myPointers, choice).address).toBe(
"$wallet.example.com/winning-pointer",
);
});

describe("pickPointer() add default weight", () => {
const noWeightPointer = [
{
address: "$wallet.example.com/zero-weight-address",
weight: 0,
},
{
address: "$wallet.example.com/undefined-weight-address",
},
];
expect(getWinningPointer(noWeightPointer, 4).address).toBe(
"$wallet.example.com/undefined-weight-address",
);
});

test("getWinningPointer returns first item in pool if choice greater than pool's weight", () => {
expect(getWinningPointer(myPointers, 1000).address).toBe("$wallet.example.com/first-pointer");
});

test("winning pointer's weight must be a number", () => {
const invalidPointer = [
{
address: "$wallet.example/test-invalid",
weight: true,
},
];
expect(() => getWinningPointer(invalidPointer, 11)).toThrow(
FundmeError(getWinningPointerMustBeANumber),
);
});
});

Expand All @@ -82,3 +123,9 @@ describe("ensure logic filters work as expected", () => {
expect(isNumberOnly("4423sd2")).toBeFalsy();
});
});

describe("convert multiple pointer pool", () => {
test("undefined parameter returns an empty array", () => {
expect(convertToPointerPool(undefined)).toEqual([]);
});
});
2 changes: 2 additions & 0 deletions tests/fund/server-side/server-side-multiple-pointers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe("multiple pointer server side", () => {

test("server-side fund() can't scrape from templates", () => {
expect(() => fund()).toThrowError(FundmeError(noUndefinedFundOnServerSide));
expect(() => fund(undefined)).toThrowError(FundmeError(noUndefinedFundOnServerSide));
expect(() => fund(null)).toThrowError(FundmeError(noUndefinedFundOnServerSide));
});

test("server-side fund() no invalid parameters", () => {
Expand Down

0 comments on commit 77edd7c

Please sign in to comment.