| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
 | describe('example.personalLog.LogCtrl', function() {
  var logCtrl;
  function createNotesCtrl() {
    var scope = angular.scope();
    scope.$cookies = scope.$service('$cookies');
    return scope.$new(example.personalLog.LogCtrl);
  }
  beforeEach(function() {
    logCtrl = createNotesCtrl();
  });
  
  it('should initialize notes with an empty array', function() {
    expect(logCtrl.logs).toEqual([]);
  });
  describe('addLog', function() {
    beforeEach(function() {
      expect(logCtrl.logs).toEqual([]);
    });
    it('should add newMsg to logs as a log entry', function() {
      logCtrl.newMsg = 'first log message';
      logCtrl.addLog();
      
      expect(logCtrl.logs.length).toBe(1);
      expect(logCtrl.logs[0].msg).toBe('first log message');
      //one more msg, this time passed in as param
      logCtrl.addLog('second log message');
      expect(logCtrl.logs.length).toBe(2);
      expect(logCtrl.logs[0].msg).toBe('first log message');
      expect(logCtrl.logs[1].msg).toBe('second log message');
    });
    it('should clear newMsg when log entry is persisted', function() {
      logCtrl.addLog('first log message');
      expect(logCtrl.newMsg).toBe('');
    });
    it('should store logs in the logs cookie', function() {
      expect(logCtrl.$cookies.logs).not.toBeDefined();
      logCtrl.addLog('first log message');
      expect(logCtrl.$cookies.logs).toBeTruthy();
    });
    it('should do nothing if newMsg is empty', function() {
      logCtrl.addLog('');
      expect(logCtrl.logs.length).toBe(0);
    });
  });
  describe('rmLog', function() {
    beforeEach(function() {
      logCtrl.addLog('message1');
      logCtrl.addLog('message2');
      logCtrl.addLog('message3');
      logCtrl.addLog('message4');
      expect(logCtrl.logs.length).toBe(4);
    });
    it('should delete a message identified by index', function() {
      logCtrl.rmLog(logCtrl.logs[1]);
      expect(logCtrl.logs.length).toBe(3);
      logCtrl.rmLog(logCtrl.logs[2]);
      expect(logCtrl.logs.length).toBe(2);
      expect(logCtrl.logs[0].msg).toBe('message1');
      expect(logCtrl.logs[1].msg).toBe('message3');
    });
    it('should update cookies when a log is deleted', function() {
      expect(logCtrl.$cookies.logs).toMatch(/\[\{.*?\}(,\{.*?\}){3}\]/);
      logCtrl.rmLog(logCtrl.logs[1]);
      expect(logCtrl.$cookies.logs).toMatch(/\[\{.*?\}(,\{.*?\}){2}\]/);
      logCtrl.rmLog(logCtrl.logs[0]);
      logCtrl.rmLog(logCtrl.logs[0]);
      logCtrl.rmLog(logCtrl.logs[0]);
      expect(logCtrl.$cookies.logs).toMatch(/\[\]/);
    });
  });
  describe('rmLogs', function() {
    beforeEach(function() {
      logCtrl.addLog('message1');
      logCtrl.addLog('message2');
      logCtrl.addLog('message3');
      logCtrl.addLog('message4');
      expect(logCtrl.logs.length).toBe(4);
    });
    it('should remove all logs', function() {
      logCtrl.rmLogs();
      expect(logCtrl.logs).toEqual([]);
    });
    it('should remove logs cookie', function() {
      expect(logCtrl.$cookies.logs).toBeTruthy();
      logCtrl.rmLogs();
      expect(logCtrl.$cookies.logs).not.toBeDefined();
    });
  });
});
 |