Skip to content
This repository has been archived by the owner on Sep 3, 2019. It is now read-only.

Commit

Permalink
added updating the existing assigned articles lists array
Browse files Browse the repository at this point in the history
  • Loading branch information
takeit committed Sep 30, 2015
1 parent 2058864 commit 79e4a20
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
29 changes: 27 additions & 2 deletions app/scripts/controllers/pane-articles-lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,34 @@
);

$scope.$on('close-articles-lists-modal', function(event) {
self.assignedArticlesLists = ArticlesList.getAllByArticle(
ArticlesList.getAllByArticle(
article.articleId, article.language
);
).$promise.then(function (data) {
// removes list from assignedArticlesLists
// if the list doesn't exist in the response data
angular.forEach(
self.assignedArticlesLists,
function (value, key) {
if (!_.some(data, {id: value.id})) {
_.remove(
self.assignedArticlesLists,
{id: value.id}
);
}
}
);

// adds a new list to assignedArticlesLists
// if it does't exist already
angular.forEach(data, function(value, key) {
if (!_.some(
self.assignedArticlesLists,
{id: value.id}
)) {
self.assignedArticlesLists.push(value);
}
});
});
});

/**
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/services/ArticlesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ angular.module('authoringEnvironmentApp').factory('ArticlesList', [
item = ArticlesList.createFromApiData(item);
articlesLists.push(item);
});
deferredGet.resolve();
deferredGet.resolve(articlesLists);
}).error(function (responseBody) {
deferredGet.reject(responseBody);
});
Expand Down
56 changes: 51 additions & 5 deletions test/spec/controllers/pane-articles-lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe('Controller: PaneArticlesListsCtrl', function () {
PaneArticlesListsCtrl,
scope,
ArticlesList,
lists,
deferredGetAll,
$modal,
$templateCache,
$httpBackend;
Expand All @@ -18,7 +20,7 @@ describe('Controller: PaneArticlesListsCtrl', function () {

beforeEach(inject(function (
$controller, $rootScope, $templateCache, _article_, _ArticlesList_,
_$modal_, _$httpBackend_
_$modal_, _$httpBackend_, $q
) {
var modalTemplate;

Expand All @@ -34,9 +36,11 @@ describe('Controller: PaneArticlesListsCtrl', function () {
}
};

spyOn(ArticlesList, 'getAllByArticle').andReturn(
[{id: 1, title: 'foo'}, {id: 4, title: 'bar'}]
);
deferredGetAll = $q.defer();
lists = [{id: 1, title: 'foo'}, {id: 4, title: 'bar'}];
lists.$promise = deferredGetAll.promise;

spyOn(ArticlesList, 'getAllByArticle').andReturn(lists);

scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
Expand Down Expand Up @@ -124,14 +128,56 @@ describe('Controller: PaneArticlesListsCtrl', function () {

it('exposes retrieved articlesLists in scope', function () {
expect(PaneArticlesListsCtrl.assignedArticlesLists).toEqual(
[{id: 1, title: 'foo'}, {id: 4, title: 'bar'}]
lists
);
});

it('$scope.$on should have been triggered', function() {
scope.$broadcast("close-articles-lists-modal");
expect(scope.$broadcast).toHaveBeenCalledWith("close-articles-lists-modal");
});

it('resolves given promise with fetched lists when an article' +
' is assigned to a new list', function () {
var existingLists = [
{id: 1, title: 'foo'},
{id: 4, title: 'bar'}
];

var newlyAddedLists = [
{id: 2, title: 'baz'}
];

var expectedData = existingLists.concat(newlyAddedLists);
PaneArticlesListsCtrl.assignedArticlesLists = existingLists;
scope.$broadcast("close-articles-lists-modal");
deferredGetAll.resolve(expectedData);
scope.$digest();

expect(scope.$broadcast).toHaveBeenCalledWith("close-articles-lists-modal");
expect(PaneArticlesListsCtrl.assignedArticlesLists).toEqual(
expectedData
);
});

it('resolves given promise with fetched lists when an article' +
' is unassigned from the existing list', function () {
var existingLists = [
{id: 1, title: 'foo'},
{id: 4, title: 'bar'}
];

var expectedData = [{id: 4, title: 'bar'}];
PaneArticlesListsCtrl.assignedArticlesLists = existingLists;
scope.$broadcast("close-articles-lists-modal");
deferredGetAll.resolve(expectedData);
scope.$digest();

expect(scope.$broadcast).toHaveBeenCalledWith("close-articles-lists-modal");
expect(PaneArticlesListsCtrl.assignedArticlesLists).toEqual(
expectedData
);
});
});

describe('scope\'s confirmUnassignArticlesList() method', function () {
Expand Down

0 comments on commit 79e4a20

Please sign in to comment.