diff --git a/dist/fluent-fix.js b/dist/fluent-fix.js index ea78dbd..9e3e858 100644 --- a/dist/fluent-fix.js +++ b/dist/fluent-fix.js @@ -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.'); diff --git a/dist/fluent-fix.spec.js b/dist/fluent-fix.spec.js index 7ec30aa..a8f4a9d 100644 --- a/dist/fluent-fix.spec.js +++ b/dist/fluent-fix.spec.js @@ -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', @@ -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 () { @@ -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"); }); }); }); diff --git a/package.json b/package.json index 195fcfe..53e99fa 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/generator.jsx b/src/generator.jsx index f822ad5..d86334d 100644 --- a/src/generator.jsx +++ b/src/generator.jsx @@ -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; } @@ -97,8 +100,7 @@ throw new Error('Generator must be of generator type.'); } - delete genFor[generator.name]; - } + delete genFor[generator.name]; } generator.removeGenerator = removeGenerator; diff --git a/test/generators.spec.jsx b/test/generators.spec.jsx index 84abc9e..5de7d19 100644 --- a/test/generators.spec.jsx +++ b/test/generators.spec.jsx @@ -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) { @@ -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 () { @@ -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"); }); }); });