Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Allowing you to test templates from MU co-located tests #33

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const parseModuleName = require('./lib/parse-module-name');

module.exports = function(babel) {
let t = babel.types;

Expand Down Expand Up @@ -39,7 +41,22 @@ module.exports = function(babel) {
}

let template = path.node.quasi.quasis.map(quasi => quasi.value.cooked).join('');
let compiledTemplateString = `Ember.HTMLBars.template(${state.opts.precompile(template)})`;

let options = {
meta: {}
}

let filename = state.file.opts.filename;
let filenameParts = filename.split('/');
filenameParts.splice(0, 1);

if (filename.includes('-components')) {
options.meta.moduleName = parseModuleName(filename);
} else if (filenameParts[2] === 'components' && filenameParts.length > 5) {
options.meta.moduleName = parseModuleName(filename);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be better if parseModuleName does the filename checking and returns null if the filename is irrelevant. So this could be

let filename = state.file.opts.filename;
let moduleName = parseModuleName(filename);
if (moduleName) {
  options.meta.moduleName = moduleName;
}

}

let compiledTemplateString = `Ember.HTMLBars.template(${state.opts.precompile(template, options)})`;

path.replaceWithSourceString(compiledTemplateString);
},
Expand Down
20 changes: 20 additions & 0 deletions lib/parse-module-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

module.exports = function (filename) {
const parts = filename.split('/');

// remove application name
parts.splice(0, 1);

const componentsIndex = parts.indexOf('-components');

if (componentsIndex > 0) {
parts.splice(componentsIndex);
} else {
parts.splice(parts.length - 2);
}

parts.push('template');

return parts.join('/') + '.hbs';
}
13 changes: 13 additions & 0 deletions tests/parse-module-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const parseModuleName = require('../lib/parse-module-name');

describe('parse-module-name helper', function() {
it('should return applicaiton template when using a route local -component', function() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo here, should read application
Also can you add a few more test for the null case?

it('should return null for public components', function() {
  expect(parseModuleName('emberconf/src/ui/components/foo-bar/component-test.js').toEqual(null);
}
it('should return null for public components in test folder', function() {
  expect(parseModuleName('emberconf/tests/integration/components/foo-bar/component-test.js').toEqual(null);
}

expect(parseModuleName('emberconf/src/ui/routes/application/-components/footer-prompt/component-test.js')).toEqual('src/ui/routes/application.hbs');
})

it('should return the parent component when using a component local component', function() {
expect(parseModuleName('emberconf/src/ui/components/icon-emberconf/facey-face/component-test.js')).toEqual('src/ui/components/icon-emberconf/template.hbs');
})
})