Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #283 from tiki/testing/unit-test-the-business-stat…
Browse files Browse the repository at this point in the history
…e-logic

Testing/unit test the business state logic
  • Loading branch information
ricardobrg authored Oct 5, 2023
2 parents bfa4050 + e022d9b commit aaf7e2f
Show file tree
Hide file tree
Showing 21 changed files with 6,129 additions and 2,133 deletions.
15 changes: 15 additions & 0 deletions __tests__/__fixtures__/connectGmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { StateAccount } from "../../src/service/store/state/state-account"
import { Account, accountTypes } from "@mytiki/capture-receipt-capacitor";
import { ServiceStore } from "../../src/service/store"

export default function connectGmail (storeGmail: StateAccount): string {
const account: Account = {
username: "[email protected]",
type: accountTypes.index.get("GMAIL")!,
isVerified: true
}
//checking the email state
storeGmail.update([account])
const gmailStatus: string = storeGmail.get().value
return gmailStatus
}
15 changes: 15 additions & 0 deletions __tests__/__fixtures__/connectRetailer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { StateAccount } from "../../src/service/store/state/state-account"
import { Account, accountTypes } from "@mytiki/capture-receipt-capacitor";
import { ServiceStore } from "../../src/service/store"

export default function connectRetailer (retailerStore: StateAccount): string {
const account: Account = {
username: "[email protected]",
type: accountTypes.index.get("AMAZON")!,
isVerified: true
}
//checking the email state
retailerStore.update([account])
const gmailStatus: string = retailerStore.get().value
return gmailStatus
}
14 changes: 14 additions & 0 deletions __tests__/__fixtures__/fiveReceipts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { StateReceipt } from "../../src/service/store/state";
export default function fiveReceipts (receiptState: StateReceipt): number {
// add 5 receipts
receiptState.add('receipt1', new Date('09/05/2023'))
receiptState.add('receipt2', new Date('09/12/2023'))
receiptState.add('receipt3', new Date('09/12/2023'))
receiptState.add('receipt4', new Date('09/19/2023'))
receiptState.add('receipt5', new Date('09/19/2023'))

//check the receipt state
const receiptCount = receiptState.count()
return receiptCount

}
10 changes: 10 additions & 0 deletions __tests__/__fixtures__/fourWeeks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { StateSync } from "../../src/service/store/state";
export default function fourWeeks (stateSync: StateSync): number{
// add the 4 weeks logins
stateSync.add(new Date('09/05/2023'))
stateSync.add(new Date('09/12/2023'))
stateSync.add(new Date('09/19/2023'))
stateSync.add(new Date('09/26/2023'))
const count = stateSync.countWeeks(new Date('09/27/2023'))
return count
}
18 changes: 18 additions & 0 deletions __tests__/__mocks__/localStorageMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let localStorageMock = (function() {
var store = {};
return {
getItem: function(key) {
return store[key];
},
setItem: function(key, value) {
store[key] = value.toString();
},
clear: function() {
store = {};
},
removeItem: function(key) {
delete store[key];
}
};
})();
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
17 changes: 17 additions & 0 deletions __tests__/__mocks__/mockCheckPayout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BulletState } from "../../src/components/bullet/bullet-state";

export default function mockCheckPayout(
sync: number,
receipts: number,
gmailState: string,
retailerState: string
): boolean {
if (
gmailState === BulletState.P100 &&
retailerState === BulletState.P100 &&
sync >= 4 &&
receipts >= 5
) {
return true
} else return false
}
31 changes: 31 additions & 0 deletions __tests__/allRequirements.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { StateAccount } from "../src/service/store/state/state-account";
import { ServiceStore } from "../src/service/store";
import connectGmail from "./__fixtures__/connectGmail";
import connectRetailer from "./__fixtures__/connectRetailer";
import fiveReceipts from "./__fixtures__/fiveReceipts";
import fourWeeks from "./__fixtures__/fourWeeks";
import mockCheckPayout from "./__mocks__/mockCheckPayout";

describe("All Requirements Fullfilled", () => {
test("Test suite", () => {
const store = new ServiceStore();
const gmail = store.gmail as StateAccount;
expect(connectGmail(gmail)).toBe("P100");

const retailer = store.retailer as StateAccount;
expect(connectRetailer(retailer)).toBe("P100");

expect(fourWeeks(store.sync)).toBe(4);

expect(fiveReceipts(store.receipt)).toBe(5);

expect(
mockCheckPayout(
store.sync.countWeeks(new Date("09/27/2023")),
store.receipt.count(),
gmail.get().value,
retailer.get().value
)
).toBeTruthy();
});
});
38 changes: 38 additions & 0 deletions __tests__/lostWeekLoginDoesNotResetState.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { StateAccount } from "../src/service/store/state/state-account";
import { ServiceStore } from "../src/service/store";
import connectGmail from "./__fixtures__/connectGmail";
import connectRetailer from "./__fixtures__/connectRetailer";
import fiveReceipts from "./__fixtures__/fiveReceipts";
import mockCheckPayout from "./__mocks__/mockCheckPayout";

describe("User misses a weekly login", () => {
test("test suite", () => {
const store = new ServiceStore();
const gmail = store.gmail as StateAccount;
const retailer = store.retailer as StateAccount;

store.sync.add(new Date("09/05/2023"));
store.sync.add(new Date("09/12/2023"));
store.sync.add(new Date("09/24/2023"));
store.sync.add(new Date("10/01/2023"));
const count = store.sync.countWeeks(new Date("10/02/2023"));

expect(count).toBeLessThan(4);

expect(connectGmail(gmail)).toBe("P100");
expect(connectRetailer(retailer)).toBe("P100");
expect(fiveReceipts(store.receipt)).toBe(5);
expect(gmail.get().value).toBe("P100")
expect(retailer.get().value).toBe("P100")
expect(store.receipt.count()).toBe(5)

expect(
mockCheckPayout(
count,
store.receipt.count(),
gmail.get().value,
retailer.get().value
)
).toBeFalsy();
});
});
30 changes: 30 additions & 0 deletions __tests__/userConnectsOnlyGmail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { StateAccount } from "../src/service/store/state/state-account";
import { ServiceStore } from "../src/service/store";
import connectGmail from "./__fixtures__/connectGmail";
import fourWeeks from "./__fixtures__/fourWeeks";
import fiveReceipts from "./__fixtures__/fiveReceipts";
import mockCheckPayout from "./__mocks__/mockCheckPayout";

describe("User connects Gmail but not Retailer Account", () => {
test("test suite", async () => {
const store = new ServiceStore();
const gmail = store.gmail as StateAccount;
expect(connectGmail(gmail)).toBe("P100");

const retailer = store.retailer as StateAccount;
await retailer.update([])

expect(fourWeeks(store.sync)).toBe(4);

expect(fiveReceipts(store.receipt)).toBe(5);

expect(
mockCheckPayout(
store.sync.countWeeks(new Date("09/27/2023")),
store.receipt.count(),
gmail.get().value,
retailer.get().value
)
).toBeFalsy();
});
});
32 changes: 32 additions & 0 deletions __tests__/userConnectsOnlyRetailer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { StateAccount } from "../src/service/store/state/state-account";
import { ServiceStore } from "../src/service/store";
import fourWeeks from "./__fixtures__/fourWeeks";
import fiveReceipts from "./__fixtures__/fiveReceipts";
import mockCheckPayout from "./__mocks__/mockCheckPayout";
import connectRetailer from "./__fixtures__/connectRetailer";

describe("User Connects Only a Retailer Account", () => {
test("test suite", async () => {
const store = new ServiceStore();
const gmail = store.gmail as StateAccount;
await gmail.update([]);

expect(gmail.get().value).toBe("NULL");

const retailer = store.retailer as StateAccount;
expect(connectRetailer(retailer)).toBe("P100");

expect(fourWeeks(store.sync)).toBe(4);

expect(fiveReceipts(store.receipt)).toBe(5);

expect(
mockCheckPayout(
store.sync.countWeeks(new Date("09/27/2023")),
store.receipt.count(),
gmail.get().value,
retailer.get().value
)
).toBeFalsy();
});
});
35 changes: 35 additions & 0 deletions __tests__/userDisconnectGmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { StateAccount } from "../src/service/store/state/state-account";
import { ServiceStore } from "../src/service/store";
import connectGmail from "./__fixtures__/connectGmail";
import connectRetailer from "./__fixtures__/connectRetailer";
import fiveReceipts from "./__fixtures__/fiveReceipts";
import fourWeeks from "./__fixtures__/fourWeeks";
import mockCheckPayout from "./__mocks__/mockCheckPayout";

describe("User disconnect Gmail", () => {
test("test suite", async () => {
const store = new ServiceStore();
const gmail = store.gmail as StateAccount;
expect(connectGmail(gmail)).toBe("P100");

await gmail.update([]);
const gmailStatus = gmail.get().value;
expect(gmailStatus).toBe("NULL");

const retailer = store.retailer as StateAccount
expect(connectRetailer(retailer)).toBe("P100");

expect(fourWeeks(store.sync)).toBe(4);

expect(fiveReceipts(store.receipt)).toBe(5);

expect(
mockCheckPayout(
store.sync.countWeeks(new Date("09/27/2023")),
store.receipt.count(),
gmail.get().value,
retailer.get().value
)
).toBeFalsy();
});
});
36 changes: 36 additions & 0 deletions __tests__/userDisconnectRetailer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

import { StateAccount } from "../src/service/store/state/state-account"
import { ServiceStore } from "../src/service/store"
import connectRetailer from "./__fixtures__/connectRetailer"
import connectGmail from "./__fixtures__/connectGmail";
import fiveReceipts from "./__fixtures__/fiveReceipts";
import fourWeeks from "./__fixtures__/fourWeeks";
import mockCheckPayout from "./__mocks__/mockCheckPayout";

describe('User disconnect Gmail', ()=>{
test("test suite", async ()=>{
const store = new ServiceStore()
const retailer = store.retailer as StateAccount
expect(connectRetailer(retailer)).toBe('P100')

await retailer.update([])
const retailerStatus = retailer.get().value
expect(retailerStatus).toBe("NULL")

const gmail = store.gmail as StateAccount
expect(connectGmail(gmail)).toBe("P100")

expect(fourWeeks(store.sync)).toBe(4);

expect(fiveReceipts(store.receipt)).toBe(5);

expect(
mockCheckPayout(
store.sync.countWeeks(new Date("09/27/2023")),
store.receipt.count(),
gmail.get().value,
retailer.get().value
)
).toBeFalsy();
})
})
38 changes: 38 additions & 0 deletions __tests__/userDoesNotShare5Receipts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { StateAccount } from "../src/service/store/state/state-account";
import { ServiceStore } from "../src/service/store";
import connectGmail from "./__fixtures__/connectGmail";
import connectRetailer from "./__fixtures__/connectRetailer";
import fourWeeks from "./__fixtures__/fourWeeks";
import mockCheckPayout from "./__mocks__/mockCheckPayout";

describe("User does not share 5 receipts", () => {
test("Test Suite", () => {
const store = new ServiceStore();

const gmail = store.gmail as StateAccount;
expect(connectGmail(gmail)).toBe("P100");

const retailer = store.retailer as StateAccount;
expect(connectRetailer(retailer)).toBe("P100");

expect(fourWeeks(store.sync)).toBe(4);

store.receipt.add("receipt1", new Date("09/05/2023"));
store.receipt.add("receipt2", new Date("09/12/2023"));
store.receipt.add("receipt3", new Date("09/12/2023"));
store.receipt.add("receipt4", new Date("09/19/2023"));

const receiptCount = store.receipt.count();

expect(receiptCount).toBe(4);

expect(
mockCheckPayout(
store.sync.countWeeks(new Date("09/27/2023")),
receiptCount,
gmail.get().value,
retailer.get().value
)
).toBeFalsy();
});
});
37 changes: 37 additions & 0 deletions __tests__/weeklyLoginMissed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { StateAccount } from "../src/service/store/state/state-account";
import { ServiceStore } from "../src/service/store";
import connectGmail from "./__fixtures__/connectGmail";
import connectRetailer from "./__fixtures__/connectRetailer";
import fiveReceipts from "./__fixtures__/fiveReceipts";
import mockCheckPayout from "./__mocks__/mockCheckPayout";

describe("User misses a weekly login", () => {
test("test suite", () => {
const store = new ServiceStore();
const gmail = store.gmail as StateAccount;

expect(connectGmail(gmail)).toBe("P100");

const retailer = store.retailer as StateAccount;
expect(connectRetailer(retailer)).toBe("P100");

expect(fiveReceipts(store.receipt)).toBe(5);

store.sync.add(new Date("09/05/2023"));
store.sync.add(new Date("09/12/2023"));
store.sync.add(new Date("09/24/2023"));
store.sync.add(new Date("10/01/2023"));
const count = store.sync.countWeeks(new Date("10/02/2023"));

expect(count).toBeLessThan(4);

expect(
mockCheckPayout(
count,
store.receipt.count(),
gmail.get().value,
retailer.get().value
)
).toBeFalsy();
});
});
Loading

0 comments on commit aaf7e2f

Please sign in to comment.