Skip to content

Instantly share code, notes, and snippets.

@timkindberg
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timkindberg/ae83c0b70bcbb2f38b54 to your computer and use it in GitHub Desktop.
Save timkindberg/ae83c0b70bcbb2f38b54 to your computer and use it in GitHub Desktop.
Quickly Mock Child Components
export function mockDirective(...names) {
return angular.mock.module(($compileProvider) => {
names.forEach((name) => {
$compileProvider.directive(name, () => {
return {
priority: 9999,
terminal: true,
restrict: 'EAC',
template:`<mock-${dashCase(name)}></mock-${dashCase(name)}>`,
replace: true
};
});
});
});
}
function dashCase(str) {
return str.replace(/([A-Z])/g, function ($1) {
return '-' + $1.toLowerCase();
});
}
// usage
beforeEach(mockDirective('myCustom', 'myOtherCustom'));
// now they appear as <mock-my-custom/> and <mock-my-other-custom/> in your parent component template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment