Projekt

Obecné

Profil

Stáhnout (6.88 KB) Statistiky
| Větev: | Tag: | Revize:
1 84a8db02 Hung Hoang
import {TestBed} from '@angular/core/testing';
2
3 fd5ab42e Hung Hoang
import {UsersService} from '../api/users.service';
4 84a8db02 Hung Hoang
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
5 fd5ab42e Hung Hoang
import {RequestStatus, VacationType} from '../../enums/common.enum';
6
import {environment} from '../../../environments/environment';
7 84a8db02 Hung Hoang
8
describe('UsersService', () => {
9
  let service: UsersService;
10
  let httpMock: HttpTestingController;
11
  const baseUrl: string = environment.apiUrl;
12
13
  beforeEach(() => {
14
    TestBed.configureTestingModule({
15
      imports: [HttpClientTestingModule],
16
      providers: [UsersService]
17
    });
18
19
    service = TestBed.get(UsersService);
20
    httpMock = TestBed.get(HttpTestingController);
21
  });
22
  afterEach(() => httpMock.verify());
23
24
25
  it('getAuthorizationRequests', () => {
26
    const dummyRequests = [
27
      { id: 1, firstName: 'Tomas', lastName: 'Novak', timestamp: '2019/05/24 21:55:11' },
28
      { id: 2, firstName: 'Tomas', lastName: 'Novak', timestamp: '2019/05/24 21:55:11' },
29
      { id: 3, firstName: 'Tomas', lastName: 'Novak', timestamp: '2019/05/24 21:55:11' },
30
      { id: 4, firstName: 'Tomas', lastName: 'Novak', timestamp: '2019/05/24 21:55:11' },
31
      { id: 5, firstName: 'Tomas', lastName: 'Novak', timestamp: '2019/05/24 21:55:11' }
32
    ];
33
34
    service.getAuthorizationRequests().subscribe((data: any) => {
35
      expect(data.length).toBe(5);
36
      expect(data[4].lastName).toBe('Novak');
37
      expect(data[4].firstName).toBe('Tomas');
38
      expect(data[4].id).toBe(5);
39
      expect(data[4].timestamp).toBeDefined();
40
    });
41
42 4bcef705 Hung Hoang
    const req = httpMock.expectOne(baseUrl + '/api/users/requests/authorization');
43 84a8db02 Hung Hoang
    expect(req.request.method).toBe('GET');
44
    req.flush(dummyRequests);
45
  });
46
47
  it('getVacationRequests', () => {
48
    const dummyRequests = [
49
        {id: 1, firstName: 'Tomas', lastName: 'Novak', date: '2019/05/24', type: 'SICKDAY', timestamp: '2019/05/24 21:59:32'},
50
        { id: 2, firstName: 'Tomas', lastName: 'Novak', date: '2019/05/24', from: '09:30', to: '18:30', type: 'VACATION', timestamp: '2019/05/24 21:59:32' },
51
        {id: 3, firstName: 'Tomas', lastName: 'Novak', date: '2019/05/24', type: 'SICKDAY', timestamp: '2019/05/24 21:59:32'},
52
        { id: 4, firstName: 'Tomas', lastName: 'Novak', date: '2019/05/24', from: '09:30', to: '18:30', type: 'VACATION', timestamp: '2019/05/24 21:59:32' },
53
        {id: 5, firstName: 'Tomas', lastName: 'Novak', date: '2019/05/24', type: 'SICKDAY', timestamp: '2019/05/24 21:59:32'}
54
      ];
55
    service.getVacationRequests().subscribe((data: any) => {
56
      expect(data.length).toBe(5);
57
58
      expect(data[0].firstName).toBeDefined();
59
      expect(data[0].lastName).toBeDefined();
60
61
      expect(data[0].type).toBe(VacationType.SICKDAY);
62
      expect(data[0].to).toBeUndefined();
63
      expect(data[0].from).toBeUndefined();
64
65
      expect(data[1].type).toBe(VacationType.VACATION);
66
      expect(data[1].to).toBeDefined();
67
      expect(data[1].from).toBeDefined();
68
    });
69
70 4bcef705 Hung Hoang
    const req = httpMock.expectOne(baseUrl + '/api/users/requests/vacation');
71 84a8db02 Hung Hoang
    expect(req.request.method).toBe('GET');
72
    req.flush(dummyRequests);
73
  });
74
75
76
  it('getAllUsers', () => {
77
    const dummyRequests = [
78
      {
79
        id: 1,
80
        firstName: 'Tomas',
81
        lastName: 'unknown',
82
        photo: 'https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg',
83
        calendar: [
84
          {
85
            id: 1,
86
            date: '2019/05/24',
87
            from: '09:00',
88
            to: '13:00',
89
            type: 'VACATION',
90
            status: 'ACCEPTED'
91
          },
92
          {
93
            id: 2,
94
            date: '2019/05/25',
95
            from: '09:00',
96
            to: '13:00',
97
            type: 'VACATION',
98
            status: 'ACCEPTED'
99
          }
100
        ]
101
      },
102
      {
103
        id: 2,
104
        firstName: 'Tomas',
105
        lastName: 'unknown',
106
        photo: 'https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg',
107
        calendar: [
108
          {
109
            id: 1,
110
            date: '2019/05/24',
111
            from: '09:00',
112
            to: '13:00',
113
            type: 'VACATION',
114
            status: 'ACCEPTED'
115
          },
116
          {
117
            id: 2,
118
            date: '2019/05/25',
119
            from: '09:00',
120
            to: '13:00',
121
            type: 'VACATION',
122
            status: 'ACCEPTED'
123
          }
124
        ]
125
      },
126
      {
127
        id: 3,
128
        firstName: 'Tomas',
129
        lastName: 'unknown',
130
        photo: 'https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg',
131
        calendar: [
132
          {
133
            id: 1,
134
            date: '2019/05/24',
135
            from: '09:00',
136
            to: '13:00',
137
            type: 'VACATION',
138
            status: 'ACCEPTED'
139
          },
140
          {
141
            id: 2,
142
            date: '2019/05/25',
143
            from: '09:00',
144
            to: '13:00',
145
            type: 'VACATION',
146
            status: 'ACCEPTED'
147
          }
148
        ]
149
      },
150
      {
151
        id: 4,
152
        firstName: 'Tomas',
153
        lastName: 'unknown',
154
        photo: 'https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg',
155
        calendar: [
156
          {
157
            id: 1,
158
            date: '2019/05/24',
159
            from: '09:00',
160
            to: '13:00',
161
            type: 'VACATION',
162
            status: 'ACCEPTED'
163
          },
164
          {
165
            id: 2,
166
            date: '2019/05/25',
167
            from: '09:00',
168
            to: '13:00',
169
            type: 'VACATION',
170
            status: 'ACCEPTED'
171
          }
172
        ]
173
      },
174
      {
175
        id: 5,
176
        firstName: 'Tomas',
177
        lastName: 'unknown',
178
        photo: 'https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg',
179
        calendar: [
180
          {
181
            id: 1,
182
            date: '2019/05/24',
183
            from: '09:00',
184
            to: '13:00',
185
            type: 'VACATION',
186
            status: 'ACCEPTED'
187
          },
188
          {
189
            id: 2,
190
            date: '2019/05/25',
191
            from: '09:00',
192
            to: '13:00',
193
            type: 'VACATION',
194
            status: 'ACCEPTED'
195
          }
196
        ]
197
      }
198
    ];
199
    service.getUsers().subscribe((data: any) => {
200
      expect(data.length).toBe(5);
201
      expect(data[0].id).toBe(1);
202
      expect(data[0].firstName).toBeDefined();
203
      expect(data[0].lastName).toBeDefined();
204
      expect(data[0].photo).toBeDefined();
205
      expect(data[0].calendar[0].type).toBe(VacationType.VACATION);
206
      expect(data[0].calendar[0].status).toBe(RequestStatus.ACCEPTED);
207
      expect(data[0].calendar[0].id).toBe(1);
208
      expect(data[0].calendar[1].id).toBe(2);
209
    });
210
211 4bcef705 Hung Hoang
    const req = httpMock.expectOne(baseUrl + '/api/users');
212 84a8db02 Hung Hoang
    expect(req.request.method).toBe('GET');
213
    req.flush(dummyRequests);
214
  });
215
216
});