Skip to content

Commit

Permalink
feat(core-test-framework): implement transfer factory
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian authored Nov 22, 2019
1 parent e323bb5 commit de75e6e
Show file tree
Hide file tree
Showing 15 changed files with 380 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ beforeEach(() => {
Factories.registerIdentityFactory(factory);
});

describe("Factory", () => {
describe("IdentityFactory", () => {
it("should make an identity with a single passphrase", () => {
const entity: Identity = factory.get("Identity").make<Identity>() as Identity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("PeerFactory", () => {
});

it("should many peers", () => {
const entities: Peer[] = factory.get("Peer").make<Peer>(5) as Peer[];
const entities: Peer[] = factory.get("Peer").makeMany<Peer>(5) as Peer[];

expect(entities).toBeArrayOfSize(5);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import "jest-extended";

import { FactoryBuilder, Factories } from "@packages/core-test-framework/src/factories";

let factory: FactoryBuilder;

beforeEach(() => {
factory = new FactoryBuilder();

Factories.registerTransactionFactory(factory);
});

describe("TransactionFactory", () => {
describe("Transfer", () => {
it("should create a transfer builder", () => {
const { data } = factory.get("Transfer").make();

expect(data.signature).toBeUndefined();
expect(data.secondSignature).toBeUndefined();
expect(data.signatures).toBeUndefined();
});

it("should create a transfer that is signed with a single passphrase", () => {
const { data } = factory
.get("Transfer")
.withStates("sign")
.make();

expect(data.signature).not.toBeUndefined();
expect(data.secondSignature).toBeUndefined();
expect(data.signatures).toBeUndefined();
});

it("should create a transfer that is signed with a second passphrase", () => {
const { data } = factory
.get("Transfer")
.withStates("sign", "secondSign")
.make();

expect(data.signature).not.toBeUndefined();
expect(data.secondSignature).not.toBeUndefined();
expect(data.signatures).toBeUndefined();
});

it("should create a transfer that is signed with multiple passphrase", () => {
const { data } = factory
.get("Transfer")
.withStates("sign", "multiSign")
.make();

expect(data.signature).not.toBeUndefined();
expect(data.secondSignature).toBeUndefined();
expect(data.signatures).not.toBeUndefined();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ beforeEach(() => {
Factories.registerWalletFactory(factory);
});

describe("Factory", () => {
describe("WalletFactory", () => {
it("should make a wallet with a single passphrase", () => {
const entity: Wallet = factory.get("Wallet").make<Wallet>() as Wallet;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ let factory: FactoryBuilder;

beforeEach(() => (factory = new FactoryBuilder()));

interface Transaction {
valid: boolean;
hooked: boolean;
verified?: boolean;
expired?: boolean;
another?: string | undefined;
}

describe("FactoryBuilder", () => {
it("should create a new entity", () => {
factory.set("Transaction", () => ({
Expand All @@ -28,13 +20,17 @@ describe("FactoryBuilder", () => {
valid: true,
}));

factory.get("Transaction").state("verified", () => ({
verified: true,
}));
factory.get("Transaction").state("verified", ({ entity }) => {
entity.verified = true;

factory.get("Transaction").state("expired", () => ({
expired: true,
}));
return entity;
});

factory.get("Transaction").state("expired", ({ entity }) => {
entity.expired = true;

return entity;
});

expect(
factory
Expand Down Expand Up @@ -65,7 +61,7 @@ describe("FactoryBuilder", () => {
valid: true,
}));

factory.get("Transaction").afterMaking((entity: Transaction) => (entity.hooked = true));
factory.get("Transaction").afterMaking(({ entity }) => (entity.hooked = true));

expect(factory.get("Transaction").make()).toEqual({
valid: true,
Expand All @@ -82,7 +78,7 @@ describe("FactoryBuilder", () => {
valid: false,
}));

factory.get("Transaction").afterMakingState("invalid", (entity: Transaction) => (entity.hooked = true));
factory.get("Transaction").afterMakingState("invalid", ({ entity }) => (entity.hooked = true));

expect(
factory
Expand All @@ -96,7 +92,7 @@ describe("FactoryBuilder", () => {
});

it("should create a new entity and respect the passed in options", () => {
factory.set("Transaction", (_, options: { valid: string }) => ({
factory.set("Transaction", ({ options }) => ({
valid: options.valid,
}));

Expand All @@ -115,7 +111,7 @@ describe("FactoryBuilder", () => {
valid: true,
}));

expect(factory.get("Transaction").make(5)).toEqual(new Array(5).fill({ valid: true }));
expect(factory.get("Transaction").makeMany(5)).toEqual(new Array(5).fill({ valid: true }));
});

it("should throw if an unknown factory is tried to be accessed", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export const registerIdentityFactory = (factory: FactoryBuilder): void => {
};
});

factory.get("Identity").state("secondPassphrase", () => {
return {
secondPassphrase: generateMnemonic(),
};
factory.get("Identity").state("secondPassphrase", ({ entity }) => {
entity.secondPassphrase = generateMnemonic();

return entity;
});
};
14 changes: 6 additions & 8 deletions packages/core-test-framework/src/factories/factories/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import { FactoryBuilder } from "../factory-builder";
export const registerPeerFactory = (factory: FactoryBuilder): void => {
const chance: Chance = new Chance();

factory.set("Peer", () => {
return {
ip: chance.ip(),
port: chance.port(),
version: chance.pickone(["3.0.0", "3.0.0-next.0"]),
latency: chance.integer({ min: 1, max: 1000 }),
};
});
factory.set("Peer", () => ({
ip: chance.ip(),
port: chance.port(),
version: chance.pickone(["3.0.0", "3.0.0-next.0"]),
latency: chance.integer({ min: 1, max: 1000 }),
}));
};
Loading

0 comments on commit de75e6e

Please sign in to comment.