UNPKG

2.66 kBJavaScriptView Raw
1describe('ng-session',function() {
2 var ngServices = factory('services/ng-services');
3 var module = factory('services/ng-session',{
4 'services/ng-services': ngServices
5 });
6
7 var mockSessionData = {
8 data: {
9 property1: 'value1',
10 property2: undefined
11 }
12 };
13 var httpMock = createHttpMock({
14 get: {
15 '/session': mockSessionData
16 }
17 });
18
19 var $session;
20
21 beforeEach(function() {
22 angular.mock.module(module.name);
23 angular.mock.module(function($provide) {
24 $provide.value('$http', httpMock);
25 });
26 angular.mock.inject(["$session", function(_$session_) {
27 $session = _$session_;
28 }]);
29 });
30
31 describe('load', function() {
32
33 it('calls http get with /session', function() {
34 $session.load();
35 expect(httpMock.get).toHaveBeenCalledWith('/session');
36 });
37
38 it('returns a promise with an object', function() {
39 $session.load().then(function(session) {
40 expect(typeof(session)).toBe("object");
41 });
42 });
43
44 it('returns a promise with an obejct containing the session properties', function() {
45 $session.load().then(function(session) {
46 expect(session.property1).toBeDefined();
47 });
48 });
49
50 });
51
52 describe('get', function() {
53
54 it('returns property\'s value when it exists', function() {
55 $session.load().then(function(session) {
56 expect($session.get('property1')).toBe('value1');
57 });
58 });
59
60 it('returns undefined when the property does\'nt exists', function() {
61 $session.load().then(function(session) {
62 expect($session.get('property3')).toBe(undefined);
63 });
64 });
65
66 });
67
68 describe('keys', function() {
69
70 it('contains key with defined value after load', function() {
71 $session.load().then(function(session) {
72 // expect($session.keys()).toEqual(['property1']);
73 expect($session.keys().includes('property1')).toBe(true);
74 });
75 });
76
77 it('contains key with undefined value after load', function() {
78 $session.load().then(function(session) {
79 expect($session.keys().includes('property2')).toBe(true);
80 });
81 });
82
83 it('does\'t contain undefined keys', function() {
84 $session.load().then(function(session) {
85 expect($session.keys().includes('property3')).toBe(false);
86 });
87 });
88
89 });
90
91});