|
1 | 1 | import { ComponentFixture, TestBed } from '@angular/core/testing';
|
2 | | - |
3 | 2 | import { UserReportComponent } from './user-report.component';
|
4 | 3 | import { UsersService } from 'src/app/shared/services/users.service';
|
| 4 | +import { of } from 'rxjs'; |
| 5 | +import { User } from 'src/app/shared/models/user.model'; |
5 | 6 | import { HttpClientTestingModule } from '@angular/common/http/testing';
|
6 | 7 | import { MaterialModule } from 'src/app/shared/material.module';
|
7 | 8 | import { NgChartsModule } from 'ng2-charts';
|
8 | 9 |
|
9 | | -describe('UserRepotComponent', () => { |
| 10 | +describe('UserReportComponent', () => { |
10 | 11 | let component: UserReportComponent;
|
11 | 12 | let fixture: ComponentFixture<UserReportComponent>;
|
| 13 | + let usersService: UsersService; |
12 | 14 |
|
13 | 15 | beforeEach(async () => {
|
14 | 16 | await TestBed.configureTestingModule({
|
15 | 17 | imports: [HttpClientTestingModule, MaterialModule, NgChartsModule],
|
16 | 18 | declarations: [UserReportComponent],
|
17 | 19 | providers: [UsersService],
|
18 | 20 | }).compileComponents();
|
| 21 | + }); |
19 | 22 |
|
| 23 | + beforeEach(() => { |
20 | 24 | fixture = TestBed.createComponent(UserReportComponent);
|
21 | 25 | component = fixture.componentInstance;
|
| 26 | + usersService = TestBed.inject(UsersService); |
22 | 27 | fixture.detectChanges();
|
23 | 28 | });
|
24 | 29 |
|
25 | | - it('should create', () => { |
26 | | - expect(component).toBeTruthy(); |
| 30 | + it('should prepare chart data correctly', () => { |
| 31 | + const mockUsers: User[] = [ |
| 32 | + { |
| 33 | + id: 1, |
| 34 | + name: 'John Doe', |
| 35 | + avatar: 'avatar_url', |
| 36 | + status: true, |
| 37 | + email: 'john@example.com', |
| 38 | + createdAt: '2022年01月01日T00:00:00', |
| 39 | + }, |
| 40 | + { |
| 41 | + id: 2, |
| 42 | + name: 'Jane Doe', |
| 43 | + avatar: 'avatar_url', |
| 44 | + status: false, |
| 45 | + email: 'jane@example.com', |
| 46 | + createdAt: '2022年02月01日T00:00:00', |
| 47 | + }, |
| 48 | + ]; |
| 49 | + |
| 50 | + spyOn(usersService, 'getUsers').and.returnValue(of(mockUsers)); |
| 51 | + spyOn(component, 'prepareChartData').and.callThrough(); |
| 52 | + |
| 53 | + component.getUsers(); |
| 54 | + |
| 55 | + expect(usersService.getUsers).toHaveBeenCalled(); |
| 56 | + |
| 57 | + expect(component.prepareChartData).toHaveBeenCalledWith(mockUsers); |
| 58 | + |
| 59 | + expect(component.barChartData.labels).toEqual(['2022']); |
| 60 | + expect(component.barChartData.datasets.length).toBe(2); |
| 61 | + expect(component.barChartData.datasets[0].label).toBe('Inactive user'); |
| 62 | + expect(component.barChartData.datasets[0].data).toEqual([1]); |
| 63 | + expect(component.barChartData.datasets[1].label).toBe('Active user'); |
| 64 | + expect(component.barChartData.datasets[1].data).toEqual([1]); |
27 | 65 | });
|
28 | 66 | });
|
0 commit comments