blob: ca9b727446219c62b6088aed1a4f31411c5562aa [file] [log] [blame]
Matteo Scandolo0968aa92015-10-23 16:31:17 +02001'use strict';
2
3describe('The Content Provider SPA', () => {
4
Matteo Scandoloba3b12a2015-10-23 17:45:45 +02005 var scope, element, isolatedScope, httpBackend, mockLocation;
Matteo Scandolo0968aa92015-10-23 16:31:17 +02006
7 // injecting main module
8 beforeEach(module('xos.contentProviderApp'));
9
10 // preload Html Templates with ng-html2js
11 beforeEach(module('templates'));
12
Matteo Scandolo0fe4c3e2015-10-23 18:37:05 +020013 beforeEach(function() {
14 module(function($provide) {
15 $provide.provider('$routeParams', function() {
16 this.$get = function() {
17 return {id: 1};
18 };
19 });
20 });
21 });
22
Matteo Scandoloba3b12a2015-10-23 17:45:45 +020023 beforeEach(inject(function(_$location_, $httpBackend) {
24 spyOn(_$location_, 'url');
25 mockLocation = _$location_;
26 httpBackend = $httpBackend;
27 // Setting up mock request
Matteo Scandolo0fe4c3e2015-10-23 18:37:05 +020028 $httpBackend.whenGET('/hpcapi/contentproviders/').respond(CPmock.CPlist);
29 $httpBackend.whenGET('/hpcapi/serviceproviders/').respond(CPmock.SPlist);
Matteo Scandoloba3b12a2015-10-23 17:45:45 +020030 $httpBackend.whenDELETE('/hpcapi/contentproviders/1/').respond();
31 }));
32
33 describe('the action directive', () => {
34 beforeEach(inject(function($compile, $rootScope) {
35 scope = $rootScope.$new();
36
37 element = angular.element('<cp-actions id="\'1\'"></cp-actions>');
38 $compile(element)(scope);
39 scope.$digest();
40 isolatedScope = element.isolateScope().vm;
41 }));
42
43 it('should delete an element and redirect to list', () => {
44 isolatedScope.deleteCp(1);
45 httpBackend.flush();
46 expect(mockLocation.url).toHaveBeenCalled();
47 });
48 });
49
Matteo Scandolo0968aa92015-10-23 16:31:17 +020050 describe('the contentProvider list', () => {
51 beforeEach(inject(function($compile, $rootScope) {
52 scope = $rootScope.$new();
53
54 element = angular.element('<content-provider-list></content-provider-list>');
55 $compile(element)(scope);
56 scope.$digest();
Matteo Scandoloba3b12a2015-10-23 17:45:45 +020057 httpBackend.flush();
58 isolatedScope = element.isolateScope().vm;
Matteo Scandolo0968aa92015-10-23 16:31:17 +020059 }));
60
61
Matteo Scandoloba3b12a2015-10-23 17:45:45 +020062 it('should load 2 contentProvider', () => {
63 expect(isolatedScope.contentProviderList.length).toBe(2);
64 });
65
66 it('should delete a contentProvider', () => {
67 isolatedScope.deleteCp(1);
68 httpBackend.flush();
69 expect(isolatedScope.contentProviderList.length).toBe(1);
Matteo Scandolo0968aa92015-10-23 16:31:17 +020070 });
71 });
Matteo Scandolo0fe4c3e2015-10-23 18:37:05 +020072
73 describe('the contentProviderDetail directive', () => {
74 describe('when an id is set in the route', () => {
75
76 beforeEach(inject(function($compile, $rootScope, ContentProvider) {
77 scope = $rootScope.$new();
78
79 httpBackend.expectGET('/hpcapi/contentproviders/1/').respond(CPmock.CPlist[0]);
80 httpBackend.whenPUT('/hpcapi/contentproviders/1/').respond({name: 'done'});
81
82 spyOn(ContentProvider, 'save').and.callThrough();
83
84 element = angular.element('<content-provider-detail></content-provider-detail>');
85 $compile(element)(scope);
86 scope.$digest();
87 httpBackend.flush();
88 isolatedScope = element.isolateScope().vm;
89 }));
90
91 it('should request the correct contentProvider', () => {
92 expect(isolatedScope.cp.name).toEqual(CPmock.CPlist[0].name);
93 });
94
95 it('should update a contentProvider', () => {
96 isolatedScope.cp.name = 'new name';
97 isolatedScope.saveContentProvider(isolatedScope.cp);
98 httpBackend.flush();
99 expect(isolatedScope.cp.name).toEqual('done');
100 });
101 });
102 });
Matteo Scandolo0968aa92015-10-23 16:31:17 +0200103});