Skip to content

Commit

Permalink
[Core] setDefaultTag() + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vinaygopinath committed Sep 20, 2016
1 parent d603a63 commit b7d8e20
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/ngMeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
//Object for storing default tag/values
var defaults = {};

//Object for storing the current route's custom meta object
var currentRouteMeta = {};

//One-time configuration
var config = {
useTitleSuffix: false
Expand Down Expand Up @@ -62,8 +59,6 @@
if (!$rootScope.ngMeta) {
throw new Error('Cannot call setTitle when ngMeta is undefined. Did you forget to call ngMeta.init() in the run block? \nRefer: https://github.com/vinaygopinath/ngMeta#getting-started');
}
currentRouteMeta.title = title;
currentRouteMeta.titleSuffix = titleSuffix;

$rootScope.ngMeta.title = angular.isDefined(title) ? title : (defaults.title || '');
if (config.useTitleSuffix) {
Expand Down Expand Up @@ -92,7 +87,6 @@
if (tag === 'title' || tag === 'titleSuffix') {
throw new Error('Attempt to set \'' + tag + '\' through \'setTag\': \'title\' and \'titleSuffix\' are reserved tag names. Please use \'ngMeta.setTitle\' instead');
}
currentRouteMeta[tag] = value;

$rootScope.ngMeta[tag] = angular.isDefined(value) ? value : defaults[tag];
return this;
Expand All @@ -118,9 +112,9 @@
defaults[tag] = value;

if (tag === 'title' || tag === 'titleSuffix') {
setTitle(currentRouteMeta.title, currentRouteMeta.titleSuffix);
this.setTitle($rootScope.ngMeta.title, $rootScope.ngMeta.titleSuffix);
} else {
setTag(tag, currentRouteMeta[tag]);
this.setTag(tag, $rootScope.ngMeta[tag]);
}

return this;
Expand All @@ -145,8 +139,6 @@
var readRouteMeta = function(meta) {
meta = meta || {};

currentRouteMeta = angular.copy(meta);

if (meta.disableUpdate) {
return false;
}
Expand Down
82 changes: 82 additions & 0 deletions test/ngMeta.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,86 @@ describe('Service: ngMeta', function() {
});
});

describe('Default tag: setDefaultTag()', function() {

describe('Basic checks', function() {
//Inject dependencies
beforeEach(function() {
injectDependencies();
});

it('should provide the setDefaultTag() function', function() {
expect(ngMeta.setDefaultTag).toBeDefined();
});

it('should throw an error when init has not been called', function() {
expect(ngMeta.setDefaultTag).toThrow();
});
});

describe('Standard Functionality', function() {

beforeEach(function() {
injectDependencies();
});

it('should set the default tag to the given value', function() {
ngMeta.init();
ngMeta.setDefaultTag(SOME_TAG, SOME_TAG_VALUE);
expect($rootScope.ngMeta[SOME_TAG]).toBe(SOME_TAG_VALUE);
});

it('should call setTitle when the default title or default titleSuffix is set', function() {
spyOn(ngMeta, 'setTitle');
ngMeta.init();
ngMeta.setDefaultTag('title', SOME_DEFAULT_TITLE);
expect(ngMeta.setTitle).toHaveBeenCalled();
});

it('should call setTag when any tag other than default title/titleSuffix is set', function() {
spyOn(ngMeta, 'setTag');
ngMeta.init();
ngMeta.setDefaultTag(SOME_TAG, SOME_TAG_DEFAULT_VALUE);
expect(ngMeta.setTag).toHaveBeenCalled();
});

it('should immediately set the current title when title is not set by route/default value', function() {
ngMeta.init();
ngMeta.setDefaultTag('title', SOME_DEFAULT_TITLE);
expect($rootScope.ngMeta.title).toEqual(SOME_DEFAULT_TITLE);
});

it('should immediately set the current tag value when tag is not set by route/default value', function() {
ngMeta.init();
ngMeta.setDefaultTag(SOME_TAG, SOME_TAG_DEFAULT_VALUE);
expect($rootScope.ngMeta[SOME_TAG]).toEqual(SOME_TAG_DEFAULT_VALUE);
});
});

describe('Custom functionality', function() {

beforeEach(function() {
module(function(ngMetaProvider) {
ngMetaProvider.setDefaultTag(SOME_TAG, SOME_TAG_DEFAULT_VALUE);
});
injectDependencies();
ngMeta.init();
});

it('should overwrite a previous default value', function() {
ngMeta.setDefaultTag(SOME_TAG, SOME_TAG_VALUE);

expect($rootScope.ngMeta[SOME_TAG]).not.toEqual(SOME_TAG_DEFAULT_VALUE);
});

it('should not overwrite a non-default value', function() {
ngMeta.setTitle(SOME_TITLE);

ngMeta.setDefaultTag('title', SOME_DEFAULT_TITLE);

expect($rootScope.ngMeta.title).toEqual(SOME_TITLE);
});
});
});

});

0 comments on commit b7d8e20

Please sign in to comment.