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

Use dummy context, update to loader-utils.getOptions #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 3 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,16 @@ function replaceStringsWithRequires(string) {
module.exports = function(source, sourcemap) {

var config = {};
var query = loaderUtils.parseQuery(this.query);
Object.assign(config, loaderUtils.getOptions(this));
var styleProperty = 'styles';
var templateProperty = 'template';

if (this.options != null) {
Object.assign(config, this.options['angular2TemplateLoader']);
}

Object.assign(config, query);

if (config.keepUrl === true) {
styleProperty = 'styleUrls';
templateProperty = 'templateUrl';
}

// Not cacheable during unit tests;
this.cacheable && this.cacheable();

this.cacheable();

var newSource = source.replace(templateUrlRegex, function (match, url) {
// replace: templateUrl: './path/to/template.html'
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
},
"homepage": "https://github.com/TheLarkInn/angular2-template-loader#readme",
"devDependencies": {
"codecov": "^1.0.1",
"istanbul": "^0.4.3",
"mocha": "^2.5.3",
"should": "^9.0.0"
"codecov": "^3.0.0",
"istanbul": "^0.4.5",
"mocha": "^5.0.4",
"should": "^13.2.1"
},
"dependencies": {
"loader-utils": "^0.2.15"
"loader-utils": "^1.1.0"
}
}
82 changes: 54 additions & 28 deletions test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,42 @@ var loader = require("../index.js");

var fixtures = require("./fixtures");

describe("loader", function() {
function getNewLoaderContext() {
return {
version: 2,
context: "",
request: "",
// query: any;
// callback: loaderCallback;
async: function(){ },
cacheable: function (flag) { },
loaders: ["angular2-template-loader"],
loaderIndex: 0,
resource: "",
resourcePath: "",
resourceQuery: "",
emitWarning: function(message) { },
emitError: function(message) { },
resolve: function(context, request, callback) { },
addDependency: function(file) { },
dependency: function(file) { },
dependency: function(file) { },
dependency: function(file) { },
dependency: function(file) { },
addContextDependency: function(directory) { },
clearDependencies: function() { },
sourceMap: true,
target: "",
webpack: true,
emitFile: function(name, content, sourceMap) {},
// fs: any,
}
}

describe("loader", function(){
it("Should convert html and style file strings to require()s", function(){

loader.call({}, fixtures.simpleAngularTestComponentFileStringSimple)
loader.call(getNewLoaderContext(), fixtures.simpleAngularTestComponentFileStringSimple)
.should
.be
.eql(`
Expand All @@ -25,7 +57,7 @@ describe("loader", function() {

it("Should convert html and style file strings to require()s regardless of inner quotes", function(){

loader.call({}, fixtures.componentWithQuoteInUrls)
loader.call(getNewLoaderContext(), fixtures.componentWithQuoteInUrls)
.should
.be
.eql(String.raw`
Expand All @@ -44,7 +76,7 @@ describe("loader", function() {

it("Should convert html and multiple style file strings to require()s", function(){

loader.call({}, fixtures.componentWithMultipleStyles)
loader.call(getNewLoaderContext(), fixtures.componentWithMultipleStyles)
.should
.be
.eql(`
Expand All @@ -65,21 +97,21 @@ describe("loader", function() {
});

it("Should return original source if there are no matches", function() {
loader.call({}, 'foo')
loader.call(getNewLoaderContext(), 'foo')
.should
.be
.eql('foo');
});

it("Should convert partial string match requires", function() {
loader.call({}, `{templateUrl: './index/app.html'}`)
loader.call(getNewLoaderContext(), `{templateUrl: './index/app.html'}`)
.should
.be
.eql(`{template: require('./index/app.html')}`);
});

it("Should handle the absense of proper relative path notation", function() {
loader.call({}, fixtures.componentWithoutRelPeriodSlash)
loader.call(getNewLoaderContext(), fixtures.componentWithoutRelPeriodSlash)
.should
.be
.eql(`
Expand All @@ -97,7 +129,7 @@ describe("loader", function() {

it("Should convert html and style file strings to require()s regardless of spacing", function(){

loader.call({}, fixtures.componentWithSpacing)
loader.call(getNewLoaderContext(), fixtures.componentWithSpacing)
.should
.be
.eql(`
Expand All @@ -114,9 +146,10 @@ describe("loader", function() {

});

it("Should keep templateUrl when asked for", function () {

loader.call({query: '?keepUrl=true'}, fixtures.componentWithSpacing)
it("Should keep templateUrl when asked using loader query", function () {
var context = getNewLoaderContext();
context.query = '?keepUrl=true';
loader.call(context, fixtures.componentWithSpacing)
.should
.be
.eql(`
Expand All @@ -133,20 +166,13 @@ describe("loader", function() {

});

it("Should keep templateUrl when asked using options", function () {

var self = {};

self.options = {
angular2TemplateLoader: {
keepUrl: true
}
};

loader.call(self, fixtures.componentWithSpacing)
.should
.be
.eql(`
it("Should keep templateUrl when asked using loader options", function () {
var context = getNewLoaderContext();
context.query = { keepUrl: true };
loader.call(context, fixtures.componentWithSpacing)
.should
.be
.eql(`
import {Component} from '@angular/core';

@Component({
Expand All @@ -156,13 +182,13 @@ describe("loader", function() {
})
export class TestComponent {}
`
);
);

});

it("Should convert html and style file strings to require()s in a single line component decorator", function() {

loader.call({}, fixtures.componentWithSingleLineDecorator)
loader.call(getNewLoaderContext(), fixtures.componentWithSingleLineDecorator)
.should
.be
.eql(`
Expand All @@ -177,7 +203,7 @@ describe("loader", function() {

it("Should convert html and style file strings to require()s if line is ending by space character", function() {

loader.call({}, fixtures.componentWithTemplateUrlEndingBySpace)
loader.call(getNewLoaderContext(), fixtures.componentWithTemplateUrlEndingBySpace)
.should
.be
.eql(`
Expand Down