Skip to content

Commit

Permalink
Test more of a public api then internal behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Krzeszowiak committed Nov 18, 2019
1 parent 21620c6 commit 8eb66b5
Showing 1 changed file with 119 additions and 80 deletions.
199 changes: 119 additions & 80 deletions dev/tests/js/jasmine/tests/lib/mage/requirejs/mixins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/

/* eslint max-nested-callbacks: 0 */
// jscs:disable jsDoc

require.config({
paths: {
'mixins': 'mage/requirejs/mixins'
Expand All @@ -13,136 +15,173 @@ require.config({
define(['rjsResolver', 'mixins'], function (resolver, mixins) {
'use strict';

var context = {
config: {}
};

describe('mixins module', function () {
beforeEach(function (done) {
spyOn(mixins, 'hasMixins').and.callThrough();
spyOn(mixins, 'getMixins').and.callThrough();
spyOn(mixins, 'load').and.callThrough();

// Wait for all modules to be loaded so they don't interfere with testing.
resolver(function () {
done();
});
});

describe('processNames method', function () {
beforeEach(function () {
spyOn(mixins, 'processNames').and.callThrough();
spyOn(mixins, 'hasMixins').and.callThrough();
});
it('does not affect modules without mixins', function (done) {
var name = 'tests/assets/mixins/no-mixins',
mixinName = 'tests/assets/mixins/no-mixins-ext';

it('gets called when module is both required and defined', function (done) {
var name = 'tests/assets/mixins/defined-module',
dependencyName = 'tests/assets/mixins/defined-module-dependency';
mixins.hasMixins.and.returnValue(false);

define(dependencyName, [], function () {});
define(name, [dependencyName], function () {});
define(name, [], function () {
return {
value: 'original'
};
});

define(mixinName, [], function () {
return function (module) {
module.value = 'changed';

require([name], function () {
expect(mixins.processNames.calls.argsFor(0)[0]).toEqual([]);
expect(mixins.processNames.calls.argsFor(1)[0]).toEqual([dependencyName]);
expect(mixins.processNames.calls.argsFor(2)[0]).toEqual([name]);
done();
});
return module;
};
});

it('keeps name intact when it already contains another plugin', function () {
mixins.hasMixins.and.returnValue(true);
require([name], function (module) {
expect(module.value).toBe('original');

expect(mixins.processNames('plugin!module', context)).toBe('plugin!module');
done();
});
});

it('does not affect modules that are loaded with plugins', function (done) {
var name = 'plugin!tests/assets/mixins/no-mixins',
mixinName = 'tests/assets/mixins/no-mixins-ext';

it('keeps name intact when it has no mixins', function () {
mixins.hasMixins.and.returnValue(false);
mixins.hasMixins.and.returnValue(true);
mixins.getMixins.and.returnValue([mixinName]);

define('plugin', [], function () {
return {
load: function (module, req, onLoad) {
req(module, onLoad);
}
};
});

expect(mixins.processNames('module', context)).toBe('module');
define(name, [], function () {
return {
value: 'original'
};
});

it('keeps names intact when they have no mixins', function () {
mixins.hasMixins.and.returnValue(false);
define(mixinName, [], function () {
return function (module) {
module.value = 'changed';

expect(mixins.processNames(['module'], context)).toEqual(['module']);
return module;
};
});

it('adds prefix to name when it has mixins', function () {
mixins.hasMixins.and.returnValue(true);
require([name], function (module) {
expect(module.value).toBe('original');

expect(mixins.processNames('module', context)).toBe('mixins!module');
done();
});
});

it('applies mixins for normal module with mixins', function (done) {
var name = 'tests/assets/mixins/mixins-applied',
mixinName = 'tests/assets/mixins/mixins-applied-ext';

it('adds prefix to name when it contains a relative path', function () {
mixins.hasMixins.and.returnValue(false);
mixins.hasMixins.and.returnValue(true);
mixins.getMixins.and.returnValue([mixinName]);

expect(mixins.processNames('./module', context)).toBe('mixins!./module');
define(name, [], function () {
return {
value: 'original'
};
});

it('adds prefix to names when they contain a relative path', function () {
mixins.hasMixins.and.returnValue(false);
define(mixinName, [], function () {
return function (module) {
module.value = 'changed';

expect(mixins.processNames(['./module'], context)).toEqual(['mixins!./module']);
return module;
};
});

it('adds prefix to names when they have mixins', function () {
mixins.hasMixins.and.returnValue(true);
require([name], function (module) {
expect(module.value).toBe('changed');

expect(mixins.processNames(['module'], context)).toEqual(['mixins!module']);
done();
});
});

describe('load method', function () {
it('is not called when module has mixins', function (done) {
var name = 'tests/assets/mixins/load-not-called';

spyOn(mixins, 'hasMixins').and.returnValue(false);
spyOn(mixins, 'load').and.callThrough();
it('applies mixins for module that is a dependency', function (done) {
var name = 'tests/assets/mixins/module-with-dependency',
dependencyName = 'tests/assets/mixins/dependency-module',
mixinName = 'tests/assets/mixins/dependency-module-ext';

define(name, [], function () {});
mixins.hasMixins.and.returnValue(true);
mixins.getMixins.and.returnValue([mixinName]);

require([name], function () {
expect(mixins.load.calls.any()).toBe(false);
done();
});
define(dependencyName, [], function () {
return {
value: 'original'
};
});

it('is called when module has mixins', function (done) {
var name = 'tests/assets/mixins/load-called';
define(name, [dependencyName], function (module) {
expect(module.value).toBe('changed');

spyOn(mixins, 'hasMixins').and.returnValue(true);
spyOn(mixins, 'load').and.callThrough();
done();

define(name, [], function () {});
return {};
});

require([name], function () {
expect(mixins.load.calls.mostRecent().args[0]).toEqual(name);
done();
});
define(mixinName, [], function () {
return function (module) {
module.value = 'changed';

return module;
};
});

it('applies mixins for loaded module', function (done) {
var name = 'tests/assets/mixins/mixins-applied',
mixinName = 'tests/assets/mixins/mixins-applied-ext';
require([name], function () {});
});

spyOn(mixins, 'hasMixins').and.returnValue(true);
spyOn(mixins, 'load').and.callThrough();
spyOn(mixins, 'getMixins').and.returnValue([mixinName]);
it('applies mixins for module that is a relative dependency', function (done) {
var name = 'tests/assets/mixins/module-with-relative-dependency',
dependencyName = 'tests/assets/mixins/relative-module',
mixinName = 'tests/assets/mixins/relative-module-ext';

define(name, [], function () {
return { value: 'original' };
});
mixins.hasMixins.and.returnValue(true);
mixins.getMixins.and.returnValue([mixinName]);

define(mixinName, [], function () {
return function(module) {
module.value = 'changed';
define(dependencyName, [], function () {
return {
value: 'original'
};
});

return module;
};
});
define(name, ['./relative-module'], function (module) {
expect(module.value).toBe('changed');

require([name], function (module) {
expect(module.value).toBe('changed');
done();
});
done();

return {};
});

define(mixinName, [], function () {
return function (module) {
module.value = 'changed';

return module;
};
});

require([name], function () {});
});
});
});

0 comments on commit 8eb66b5

Please sign in to comment.