Skip to content

Commit

Permalink
Convert remaining factory names to fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Bogart committed Sep 15, 2020
1 parent d9cd816 commit e07457d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
6 changes: 3 additions & 3 deletions lib/definition-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import {isFunction, last} from "lodash";

export class DefinitionProxy {
definition: Definition;
childFactories: any[];
childFixtures: any[];
ignore: boolean;

constructor(definition: Definition, ignore = false) {
this.definition = definition;
this.childFactories = [];
this.childFixtures = [];
this.ignore = ignore;
}

Expand Down Expand Up @@ -74,7 +74,7 @@ export class DefinitionProxy {
fixture(name: string, model: object, rest?: FixtureOptions | blockFunction): void;
fixture(name: string, model: object, options: FixtureOptions, block?: blockFunction): void;
fixture(name: string, model: object, ...rest: any[]): void {
this.childFactories.push([name, model, ...rest]);
this.childFixtures.push([name, model, ...rest]);
}

association(name: string, ...rest: any[]): void {
Expand Down
12 changes: 6 additions & 6 deletions lib/fixture-riveter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function extractAttributes(traitsAndOptions: any[]): Record<string, any>
}

export class FixtureRiveter {
factories: Record<string, Fixture>;
fixtures: Record<string, Fixture>;
traits: Record<string, Trait>;
adapterHandler: any;
sequenceHandler: SequenceHandler;
Expand All @@ -45,7 +45,7 @@ export class FixtureRiveter {
strategyHandler: StrategyHandler;

constructor() {
this.factories = {};
this.fixtures = {};
this.traits = {};
this.adapterHandler = new AdapterHandler();
this.sequenceHandler = new SequenceHandler();
Expand All @@ -65,7 +65,7 @@ export class FixtureRiveter {
}

getFixture(name: string, throws = true): Fixture {
const fixture = this.factories[name];
const fixture = this.fixtures[name];
if (throws && !fixture) {
throw new Error(`${name} hasn't been defined yet`);
}
Expand All @@ -74,7 +74,7 @@ export class FixtureRiveter {

registerFixture(fixture: Fixture): void {
for (const name of fixture.names()) {
this.factories[name] = fixture;
this.fixtures[name] = fixture;
}
}

Expand All @@ -97,7 +97,7 @@ export class FixtureRiveter {
proxy.execute();
this.registerFixture(fixture);

proxy.childFactories.forEach((child: any[]) => {
proxy.childFixtures.forEach((child: any[]) => {
const [childName, childModel, ...childRest] = child;
const [childOptions, childBlock] = fixtureOptionsParser(...childRest);
childOptions.parent = childOptions.parent || name;
Expand Down Expand Up @@ -138,7 +138,7 @@ export class FixtureRiveter {

resetSequences(): void {
this.sequenceHandler.resetSequences();
for (const [, fixture] of Object.entries(this.factories)) {
for (const [, fixture] of Object.entries(this.fixtures)) {
fixture.sequenceHandler.resetSequences();
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export class Trait extends Definition {
const proxy = new DefinitionProxy(this);
proxy.execute();

if (proxy.childFactories.length > 0) {
const [factory] = proxy.childFactories;
throw new Error(`Can't define a factory (${factory.name}) inside trait (${this.name})`);
if (proxy.childFixtures.length > 0) {
const [fixture] = proxy.childFixtures;
throw new Error(`Can't define a fixture (${fixture.name}) inside trait (${this.name})`);
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/unit/definition-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ describe("DefinitionProxy", function() {
expect(proxy.definition).to.equal(fixture);
});

it("creates an array for child factories", function() {
it("creates an array for child fixtures", function() {
const fixtureRiveter = new FixtureRiveter();
const fixture = new Fixture(fixtureRiveter, "dummy", DummyModel);
const proxy = new DefinitionProxy(fixture);
expect(proxy.childFactories).to.deep.equal([]);
expect(proxy.childFixtures).to.deep.equal([]);
});

describe("#execute", function() {
Expand Down Expand Up @@ -98,7 +98,7 @@ describe("DefinitionProxy", function() {
const proxy = new DefinitionProxy(fixture);
proxy.fixture("newFixture", DummyModel);

expect(proxy.childFactories).to.deep.equal([["newFixture", DummyModel]]);
expect(proxy.childFixtures).to.deep.equal([["newFixture", DummyModel]]);
});
});

Expand Down
28 changes: 14 additions & 14 deletions test/unit/fixture-riveter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("FixtureRiveter", function() {
it("can be built", function() {
const fixtureRiveter = new FixtureRiveter();
expect(fixtureRiveter).to.exist;
expect(fixtureRiveter.factories).to.exist.and.to.be.empty;
expect(fixtureRiveter.fixtures).to.exist.and.to.be.empty;
});

describe("#getAdapter", function() {
Expand Down Expand Up @@ -65,9 +65,9 @@ describe("FixtureRiveter", function() {
it("calls the block immediately", function() {
const fixtureRiveter = new FixtureRiveter();
const testArray = ["test"] as any;
fixtureRiveter.factories = testArray;
fixtureRiveter.fixtures = testArray;

expect(fixtureRiveter.factories).to.deep.equal(testArray);
expect(fixtureRiveter.fixtures).to.deep.equal(testArray);
});
});

Expand All @@ -79,7 +79,7 @@ describe("FixtureRiveter", function() {

fixtureRiveter.registerFixture(fixture);

expect(fixtureRiveter.factories[name]).to.equal(fixture);
expect(fixtureRiveter.fixtures[name]).to.equal(fixture);
});

it("adds the fixture by alias", function() {
Expand All @@ -90,8 +90,8 @@ describe("FixtureRiveter", function() {

fixtureRiveter.registerFixture(fixture);

expect(fixtureRiveter.factories[aliases[0]]).to.equal(fixture);
expect(fixtureRiveter.factories[aliases[1]]).to.equal(fixture);
expect(fixtureRiveter.fixtures[aliases[0]]).to.equal(fixture);
expect(fixtureRiveter.fixtures[aliases[1]]).to.equal(fixture);
});

it("adds the same fixture multiples times", function() {
Expand All @@ -102,9 +102,9 @@ describe("FixtureRiveter", function() {

fixtureRiveter.registerFixture(fixture);

const {factories} = fixtureRiveter;
const {fixtures} = fixtureRiveter;

expect(factories[name]).to.deep.equal(factories[alias]);
expect(fixtures[name]).to.deep.equal(fixtures[alias]);
});
});

Expand All @@ -114,9 +114,9 @@ describe("FixtureRiveter", function() {
const name = "testFixture";
fixtureRiveter.fixture(name, DummyModel);

const fixture = fixtureRiveter.factories[name];
const fixture = fixtureRiveter.fixtures[name];

expect(fixtureRiveter.factories).to.not.be.empty;
expect(fixtureRiveter.fixtures).to.not.be.empty;
expect(fixture).to.exist;
expect(fixture.name).to.equal(name);
});
Expand Down Expand Up @@ -158,14 +158,14 @@ describe("FixtureRiveter", function() {
expect(testFn).to.throw();
});

it("creates child factories", function() {
it("creates child fixtures", function() {
const fixtureRiveter = new FixtureRiveter();
fixtureRiveter.fixture("user", DummyModel, (f: any) => {
f.fixture("oldUser", DummyModel);
});
const result = Object
.keys(fixtureRiveter.factories)
.map((name: string) => fixtureRiveter.factories[name])
.keys(fixtureRiveter.fixtures)
.map((name: string) => fixtureRiveter.fixtures[name])
.map((f: Fixture) => f.name);
expect(result).to.deep.equal(["user", "oldUser"]);
});
Expand All @@ -177,7 +177,7 @@ describe("FixtureRiveter", function() {
const fixture = new FixtureRiveter();
fixture.fixture(name, DummyModel);
const t = fixture.getFixture(name);
const result = fixture.factories[name];
const result = fixture.fixtures[name];
expect(t).to.equal(result);
});

Expand Down

0 comments on commit e07457d

Please sign in to comment.