Skip to content

Commit

Permalink
Custom generators (#25)
Browse files Browse the repository at this point in the history
Fix the tests for the custom generators.
  • Loading branch information
jasond-s authored Sep 7, 2016
1 parent 790f659 commit 85af7e5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
5 changes: 4 additions & 1 deletion dist/fluent-fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,14 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

generator.Object = ObjectGenerator;

/* Custom generators
/* Generator globals
************************************************************/

var genFor = generator.For || {};

/* Custom generators
************************************************************/

function addGenerator(generator) {
if (!(new generator() instanceof GeneratorBase)) {
throw new Error('Generator must be of generator type.');
Expand Down
20 changes: 16 additions & 4 deletions dist/fluent-fix.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,14 @@ describe('Generators for fixture values', function () {
_classCallCheck(this, Test);

_get(Object.getPrototypeOf(Test.prototype), 'constructor', this).call(this);

this.name = "TEST_GENERATOR";
}

_createClass(Test, [{
key: 'generate',
value: function generate() {
return testValue;
return "SOME_RANDOM_VALUE";
}
}], [{
key: 'match',
Expand All @@ -622,15 +624,25 @@ describe('Generators for fixture values', function () {
it('should call gen when used in fixture', function () {
var testClass = fixture();

expect(testClass.something.test).toEqual(testValue.test);
expect(testClass.something).toEqual("SOME_RANDOM_VALUE");
});

it('should remove gen', function () {
FluentFix.Generator.removeGenerator(Test);

// The fixture caches all the generators in the structure of the fixture.
// So. We need to regenerate the fixture after editing the globals.
fixture = FluentFix.fixture({
something: {
test: 'TEST_VALUE'
}
});

var testClass = fixture();

expect(testClass.something.test).toEqual(jasmine.any(String));
console.log(testClass.something);

expect(testClass.something).toEqual({ test: jasmine.any(String) });
});

describe('used directly', function () {
Expand All @@ -646,7 +658,7 @@ describe('Generators for fixture values', function () {
it('should call gen when used in fixture', function () {
var testClass = directfixture();

expect(testClass.something.test).toEqual(testValue.test);
expect(testClass.something).toEqual("SOME_RANDOM_VALUE");
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fluent-fix",
"version": "0.3.0-alpha",
"version": "0.3.1-beta",
"description": "A simple fluent builder for test fixtures.",
"scripts": {
"test": "karma start karma.conf.js",
Expand Down
10 changes: 6 additions & 4 deletions src/generator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,19 @@
generator.Object = ObjectGenerator;


/* Custom generators
/* Generator globals
************************************************************/

let genFor = generator.For || {};


/* Custom generators
************************************************************/

function addGenerator (generator) {
if (!(new generator() instanceof GeneratorBase)) {
throw new Error('Generator must be of generator type.');
}
}

genFor[generator.name] = generator;
}
Expand All @@ -97,8 +100,7 @@
throw new Error('Generator must be of generator type.');
}

delete genFor[generator.name];
}
delete genFor[generator.name]; }

generator.removeGenerator = removeGenerator;

Expand Down
24 changes: 19 additions & 5 deletions test/generators.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,14 @@ describe('Generators for fixture values', function () {
};

class Test extends FluentFix.Generator.Abstract {
constructor () { super() }
constructor () {
super();

this.name = "TEST_GENERATOR";
}

generate () {
return testValue;
return "SOME_RANDOM_VALUE";
}

static match (property) {
Expand All @@ -284,15 +288,25 @@ describe('Generators for fixture values', function () {
it('should call gen when used in fixture', function () {
let testClass = fixture();

expect(testClass.something.test).toEqual(testValue.test);
expect(testClass.something).toEqual("SOME_RANDOM_VALUE");
});

it('should remove gen', function () {
FluentFix.Generator.removeGenerator(Test);

// The fixture caches all the generators in the structure of the fixture.
// So. We need to regenerate the fixture after editing the globals.
fixture = FluentFix.fixture({
something: {
test: 'TEST_VALUE'
}
});

let testClass = fixture();

expect(testClass.something.test).toEqual(jasmine.any(String));
console.log(testClass.something);

expect(testClass.something).toEqual({ test: jasmine.any(String) });
});

describe('used directly', function () {
Expand All @@ -308,7 +322,7 @@ describe('Generators for fixture values', function () {
it('should call gen when used in fixture', function () {
let testClass = directfixture();

expect(testClass.something.test).toEqual(testValue.test);
expect(testClass.something).toEqual("SOME_RANDOM_VALUE");
});
});
});
Expand Down

0 comments on commit 85af7e5

Please sign in to comment.