Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added merge method to extension, updated package and misc. #19

Merged
merged 2 commits into from
Apr 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mixtape/core",
"version": "0.2.2",
"version": "1.0.0",
"description": "Supercharged fixture library for organizing and generating test data",
"main": "lib/index.js",
"scripts": {
Expand All @@ -25,7 +25,7 @@
"jest": "^24.7.1",
"ts-jest": "^24.0.2",
"tslint": "^5.16.0",
"typescript": "^3.4.3",
"typescript": "^3.4.5",
"validator": "^10.11.0"
},
"files": [
Expand All @@ -36,13 +36,13 @@
"keywords": [
"test",
"testing",
"tdd",
"fixture",
"unit",
"integration",
"fixture",
"autofixture",
"factory",
"builder",
"tdd",
"data",
"generator",
"automation",
Expand Down
31 changes: 31 additions & 0 deletions src/__tests__/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,37 @@ describe('Extension', () => {
expect(sut.builders.length).toBe(0);
});

test('should merge extension', () => {
// Arrange
const builderTypeToMerge = uuid();
const builderTypeAliasToMerge = uuid();
const dummyBuilderToMerge: TypeBuilder<any> = {
type: builderTypeToMerge,
aliases: [builderTypeAliasToMerge],
build: undefined
};
const extensionToMerge = new Extension();
extensionToMerge.add(dummyBuilderToMerge);
const existingBuilderType = uuid();
const existingBuilderAlias = uuid();
const existingDummyBuilder: TypeBuilder<any> = {
aliases: [existingBuilderAlias],
type: existingBuilderType,
build: undefined
};
const sut = new Extension();
sut.add(existingDummyBuilder);

// Act
sut.merge(extensionToMerge);

// Assert
expect(sut.get(existingBuilderType)).toEqual(existingDummyBuilder);
expect(sut.get(existingBuilderAlias)).toEqual(existingDummyBuilder);
expect(sut.get(builderTypeToMerge)).toEqual(dummyBuilderToMerge);
expect(sut.get(builderTypeAliasToMerge)).toEqual(dummyBuilderToMerge);
});

test('should clear added builders', () => {
// Arrange
const sut = new Extension();
Expand Down
1 change: 0 additions & 1 deletion src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export abstract class Builder<T> implements TypeBuilder<T> {
* Register new alias for type, e.g. 'surname' and 'lastName'
* @protected
* @param alias - alias for type
* @param type - type (use the one from builder)
*/
protected createAlias(alias: string) {
this.aliases.push(alias);
Expand Down
10 changes: 8 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { TypeBuilder } from './builder';
import { ensure } from './utils';

/**
* Class for bundling builders together
* Bundling builders together in extension to make it easy to add to `Fixture`s.
* Class for bundling builders
* Bundle builders in an extension to make it easy to add to a given `Fixture`.
*/
export class Extension {
private _builders: {[type: string]: TypeBuilder<any>};
Expand Down Expand Up @@ -75,6 +75,12 @@ export class Extension {
return this;
}

merge(extension: Extension): this {
extension.builders.forEach(b => this.add(b));

return this;
}

/**
* Remove all builders (and aliases)
*/
Expand Down
2 changes: 1 addition & 1 deletion src/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Fixture implements FixtureContext {
* @returns `this`
*/
extend(extension: Extension): this {
extension.builders.forEach(b => this._extensions.add(b));
this._extensions.merge(extension);

return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/object-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class ObjectBuilder<T extends object> {
if (isArray(value)) {
ensure(
() => value.length === 1 && typeof value[0] === 'string',
'Array in template should only contain one type (string)');
'Array in template should (only) contain one type');
o[k] = context.createMany(value[0] as string);
return o;
}
Expand Down