Projekt

Obecné

Profil

Stáhnout (6.93 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {TestBed} from '@angular/core/testing';
2

    
3
import {UsersService} from './users.service';
4
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
5
import {VacationRequest} from '../models/requests.model';
6
import {RequestStatus, VacationType} from '../enums/common.enum';
7
import {environment} from '../../environments/environment';
8

    
9
describe('UsersService', () => {
10
  let service: UsersService;
11
  let httpMock: HttpTestingController;
12
  const baseUrl: string = environment.apiUrl;
13

    
14
  beforeEach(() => {
15
    TestBed.configureTestingModule({
16
      imports: [HttpClientTestingModule],
17
      providers: [UsersService]
18
    });
19

    
20
    service = TestBed.get(UsersService);
21
    httpMock = TestBed.get(HttpTestingController);
22
  });
23
  afterEach(() => httpMock.verify());
24

    
25

    
26
  it('getAuthorizationRequests', () => {
27
    const dummyRequests = [
28
      { id: 1, firstName: 'Tomas', lastName: 'Novak', timestamp: '2019/05/24 21:55:11' },
29
      { id: 2, firstName: 'Tomas', lastName: 'Novak', timestamp: '2019/05/24 21:55:11' },
30
      { id: 3, firstName: 'Tomas', lastName: 'Novak', timestamp: '2019/05/24 21:55:11' },
31
      { id: 4, firstName: 'Tomas', lastName: 'Novak', timestamp: '2019/05/24 21:55:11' },
32
      { id: 5, firstName: 'Tomas', lastName: 'Novak', timestamp: '2019/05/24 21:55:11' }
33
    ];
34

    
35
    service.getAuthorizationRequests().subscribe((data: any) => {
36
      expect(data.length).toBe(5);
37
      expect(data[4].lastName).toBe('Novak');
38
      expect(data[4].firstName).toBe('Tomas');
39
      expect(data[4].id).toBe(5);
40
      expect(data[4].timestamp).toBeDefined();
41
    });
42

    
43
    const req = httpMock.expectOne(baseUrl + '/api/users/requests/authorization?');
44
    expect(req.request.method).toBe('GET');
45
    req.flush(dummyRequests);
46
  });
47

    
48
  it('getVacationRequests', () => {
49
    const dummyRequests = [
50
        {id: 1, firstName: 'Tomas', lastName: 'Novak', date: '2019/05/24', type: 'SICKDAY', timestamp: '2019/05/24 21:59:32'},
51
        { 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' },
52
        {id: 3, firstName: 'Tomas', lastName: 'Novak', date: '2019/05/24', type: 'SICKDAY', timestamp: '2019/05/24 21:59:32'},
53
        { 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' },
54
        {id: 5, firstName: 'Tomas', lastName: 'Novak', date: '2019/05/24', type: 'SICKDAY', timestamp: '2019/05/24 21:59:32'}
55
      ];
56
    service.getVacationRequests().subscribe((data: any) => {
57
      expect(data.length).toBe(5);
58

    
59
      expect(data[0].firstName).toBeDefined();
60
      expect(data[0].lastName).toBeDefined();
61

    
62
      expect(data[0].type).toBe(VacationType.SICKDAY);
63
      expect(data[0].to).toBeUndefined();
64
      expect(data[0].from).toBeUndefined();
65

    
66
      expect(data[1].type).toBe(VacationType.VACATION);
67
      expect(data[1].to).toBeDefined();
68
      expect(data[1].from).toBeDefined();
69
    });
70

    
71
    const req = httpMock.expectOne(baseUrl + '/api/users/requests/vacation?');
72
    expect(req.request.method).toBe('GET');
73
    req.flush(dummyRequests);
74
  });
75

    
76

    
77
  it('getAllUsers', () => {
78
    const dummyRequests = [
79
      {
80
        id: 1,
81
        firstName: 'Tomas',
82
        lastName: 'unknown',
83
        photo: 'https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg',
84
        calendar: [
85
          {
86
            id: 1,
87
            date: '2019/05/24',
88
            from: '09:00',
89
            to: '13:00',
90
            type: 'VACATION',
91
            status: 'ACCEPTED'
92
          },
93
          {
94
            id: 2,
95
            date: '2019/05/25',
96
            from: '09:00',
97
            to: '13:00',
98
            type: 'VACATION',
99
            status: 'ACCEPTED'
100
          }
101
        ]
102
      },
103
      {
104
        id: 2,
105
        firstName: 'Tomas',
106
        lastName: 'unknown',
107
        photo: 'https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg',
108
        calendar: [
109
          {
110
            id: 1,
111
            date: '2019/05/24',
112
            from: '09:00',
113
            to: '13:00',
114
            type: 'VACATION',
115
            status: 'ACCEPTED'
116
          },
117
          {
118
            id: 2,
119
            date: '2019/05/25',
120
            from: '09:00',
121
            to: '13:00',
122
            type: 'VACATION',
123
            status: 'ACCEPTED'
124
          }
125
        ]
126
      },
127
      {
128
        id: 3,
129
        firstName: 'Tomas',
130
        lastName: 'unknown',
131
        photo: 'https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg',
132
        calendar: [
133
          {
134
            id: 1,
135
            date: '2019/05/24',
136
            from: '09:00',
137
            to: '13:00',
138
            type: 'VACATION',
139
            status: 'ACCEPTED'
140
          },
141
          {
142
            id: 2,
143
            date: '2019/05/25',
144
            from: '09:00',
145
            to: '13:00',
146
            type: 'VACATION',
147
            status: 'ACCEPTED'
148
          }
149
        ]
150
      },
151
      {
152
        id: 4,
153
        firstName: 'Tomas',
154
        lastName: 'unknown',
155
        photo: 'https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg',
156
        calendar: [
157
          {
158
            id: 1,
159
            date: '2019/05/24',
160
            from: '09:00',
161
            to: '13:00',
162
            type: 'VACATION',
163
            status: 'ACCEPTED'
164
          },
165
          {
166
            id: 2,
167
            date: '2019/05/25',
168
            from: '09:00',
169
            to: '13:00',
170
            type: 'VACATION',
171
            status: 'ACCEPTED'
172
          }
173
        ]
174
      },
175
      {
176
        id: 5,
177
        firstName: 'Tomas',
178
        lastName: 'unknown',
179
        photo: 'https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg',
180
        calendar: [
181
          {
182
            id: 1,
183
            date: '2019/05/24',
184
            from: '09:00',
185
            to: '13:00',
186
            type: 'VACATION',
187
            status: 'ACCEPTED'
188
          },
189
          {
190
            id: 2,
191
            date: '2019/05/25',
192
            from: '09:00',
193
            to: '13:00',
194
            type: 'VACATION',
195
            status: 'ACCEPTED'
196
          }
197
        ]
198
      }
199
    ];
200
    service.getUsers().subscribe((data: any) => {
201
      expect(data.length).toBe(5);
202
      expect(data[0].id).toBe(1);
203
      expect(data[0].firstName).toBeDefined();
204
      expect(data[0].lastName).toBeDefined();
205
      expect(data[0].photo).toBeDefined();
206
      expect(data[0].calendar[0].type).toBe(VacationType.VACATION);
207
      expect(data[0].calendar[0].status).toBe(RequestStatus.ACCEPTED);
208
      expect(data[0].calendar[0].id).toBe(1);
209
      expect(data[0].calendar[1].id).toBe(2);
210
    });
211

    
212
    const req = httpMock.expectOne(baseUrl + '/api/users?');
213
    expect(req.request.method).toBe('GET');
214
    req.flush(dummyRequests);
215
  });
216

    
217
});
(6-6/7)