aboutsummaryrefslogtreecommitdiffstats
path: root/test/directive/ngPluralizeSpec.js
blob: c7766c7b62f5f9f47e5ec3b7e6e4e706af012aef (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';

describe('ng-pluralize', function() {
  var element;


  afterEach(function(){
    dealoc(element);
  });


  describe('deal with pluralized strings without offset', function() {
     beforeEach(inject(function($rootScope, $compile) {
        element = $compile(
          '<ng:pluralize count="email"' +
                         "when=\"{'0': 'You have no new email'," +
                                 "'one': 'You have one new email'," +
                                 "'other': 'You have {} new emails'}\">" +
          '</ng:pluralize>')($rootScope);
      }));


      it('should show single/plural strings', inject(function($rootScope) {
        $rootScope.email = 0;
        $rootScope.$digest();
        expect(element.text()).toBe('You have no new email');

        $rootScope.email = '0';
        $rootScope.$digest();
        expect(element.text()).toBe('You have no new email');

        $rootScope.email = 1;
        $rootScope.$digest();
        expect(element.text()).toBe('You have one new email');

        $rootScope.email = 0.01;
        $rootScope.$digest();
        expect(element.text()).toBe('You have 0.01 new emails');

        $rootScope.email = '0.1';
        $rootScope.$digest();
        expect(element.text()).toBe('You have 0.1 new emails');

        $rootScope.email = 2;
        $rootScope.$digest();
        expect(element.text()).toBe('You have 2 new emails');

        $rootScope.email = -0.1;
        $rootScope.$digest();
        expect(element.text()).toBe('You have -0.1 new emails');

        $rootScope.email = '-0.01';
        $rootScope.$digest();
        expect(element.text()).toBe('You have -0.01 new emails');

        $rootScope.email = -2;
        $rootScope.$digest();
        expect(element.text()).toBe('You have -2 new emails');
      }));


      it('should show single/plural strings with mal-formed inputs', inject(function($rootScope) {
        $rootScope.email = '';
        $rootScope.$digest();
        expect(element.text()).toBe('');

        $rootScope.email = null;
        $rootScope.$digest();
        expect(element.text()).toBe('');

        $rootScope.email = undefined;
        $rootScope.$digest();
        expect(element.text()).toBe('');

        $rootScope.email = 'a3';
        $rootScope.$digest();
        expect(element.text()).toBe('');

        $rootScope.email = '011';
        $rootScope.$digest();
        expect(element.text()).toBe('You have 11 new emails');

        $rootScope.email = '-011';
        $rootScope.$digest();
        expect(element.text()).toBe('You have -11 new emails');

        $rootScope.email = '1fff';
        $rootScope.$digest();
        expect(element.text()).toBe('You have one new email');

        $rootScope.email = '0aa22';
        $rootScope.$digest();
        expect(element.text()).toBe('You have no new email');

        $rootScope.email = '000001';
        $rootScope.$digest();
        expect(element.text()).toBe('You have one new email');
      }));
  });


  describe('deal with pluralized strings with offset', function() {
    it('should show single/plural strings with offset', inject(function($rootScope, $compile) {
      element = $compile(
        "<ng:pluralize count=\"viewCount\"  offset=2 " +
            "when=\"{'0': 'Nobody is viewing.'," +
                    "'1': '{{p1}} is viewing.'," +
                    "'2': '{{p1}} and {{p2}} are viewing.'," +
                    "'one': '{{p1}}, {{p2}} and one other person are viewing.'," +
                    "'other': '{{p1}}, {{p2}} and {} other people are viewing.'}\">" +
        "</ng:pluralize>")($rootScope);
      $rootScope.p1 = 'Igor';
      $rootScope.p2 = 'Misko';

      $rootScope.viewCount = 0;
      $rootScope.$digest();
      expect(element.text()).toBe('Nobody is viewing.');

      $rootScope.viewCount = 1;
      $rootScope.$digest();
      expect(element.text()).toBe('Igor is viewing.');

      $rootScope.viewCount = 2;
      $rootScope.$digest();
      expect(element.text()).toBe('Igor and Misko are viewing.');

      $rootScope.viewCount = 3;
      $rootScope.$digest();
      expect(element.text()).toBe('Igor, Misko and one other person are viewing.');

      $rootScope.viewCount = 4;
      $rootScope.$digest();
      expect(element.text()).toBe('Igor, Misko and 2 other people are viewing.');
    }));
  });
});