juice-shop

Форк
0
/
wallet.component.spec.ts 
103 строки · 3.7 Кб
1
/*
2
 * Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3
 * SPDX-License-Identifier: MIT
4
 */
5

6
import { TranslateModule, TranslateService } from '@ngx-translate/core'
7
import { HttpClientTestingModule } from '@angular/common/http/testing'
8
import { MatCardModule } from '@angular/material/card'
9
import { MatFormFieldModule } from '@angular/material/form-field'
10
import { type ComponentFixture, fakeAsync, TestBed, waitForAsync } from '@angular/core/testing'
11
import { MatInputModule } from '@angular/material/input'
12
import { ReactiveFormsModule } from '@angular/forms'
13
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
14

15
import { of, throwError } from 'rxjs'
16
import { RouterTestingModule } from '@angular/router/testing'
17
import { MatGridListModule } from '@angular/material/grid-list'
18
import { WalletComponent } from './wallet.component'
19
import { WalletService } from '../Services/wallet.service'
20
import { EventEmitter } from '@angular/core'
21
import { MatSnackBar } from '@angular/material/snack-bar'
22

23
describe('WalletComponent', () => {
24
  let component: WalletComponent
25
  let fixture: ComponentFixture<WalletComponent>
26
  let walletService
27
  let translateService
28
  let snackBar: any
29

30
  beforeEach(waitForAsync(() => {
31
    walletService = jasmine.createSpyObj('AddressService', ['get', 'put'])
32
    walletService.get.and.returnValue(of({}))
33
    walletService.put.and.returnValue(of({}))
34
    translateService = jasmine.createSpyObj('TranslateService', ['get'])
35
    translateService.get.and.returnValue(of({}))
36
    translateService.onLangChange = new EventEmitter()
37
    translateService.onTranslationChange = new EventEmitter()
38
    translateService.onDefaultLangChange = new EventEmitter()
39
    snackBar = jasmine.createSpyObj('MatSnackBar', ['open'])
40

41
    TestBed.configureTestingModule({
42
      imports: [
43
        RouterTestingModule,
44
        TranslateModule.forRoot(),
45
        HttpClientTestingModule,
46
        ReactiveFormsModule,
47

48
        BrowserAnimationsModule,
49
        MatCardModule,
50
        MatFormFieldModule,
51
        MatInputModule,
52
        MatGridListModule
53
      ],
54
      declarations: [WalletComponent],
55
      providers: [
56
        { provide: WalletService, useValue: walletService },
57
        { provide: TranslateService, useValue: translateService },
58
        { provide: MatSnackBar, useValue: snackBar }
59
      ]
60
    })
61
      .compileComponents()
62
  }))
63

64
  beforeEach(() => {
65
    fixture = TestBed.createComponent(WalletComponent)
66
    component = fixture.componentInstance
67
    fixture.detectChanges()
68
  })
69

70
  it('should create', () => {
71
    expect(component).toBeTruthy()
72
  })
73

74
  it('should be compulsory to provide amount', () => {
75
    component.balanceControl.setValue('')
76
    expect(component.balanceControl.valid).toBeFalsy()
77
  })
78

79
  it('amount should be in the range [10, 1000]', () => {
80
    component.balanceControl.setValue(-1)
81
    expect(component.balanceControl.valid).toBeFalsy()
82
    component.balanceControl.setValue(10000000000)
83
    expect(component.balanceControl.valid).toBeFalsy()
84
    component.balanceControl.setValue(10)
85
    expect(component.balanceControl.valid).toBe(true)
86
    component.balanceControl.setValue(1000)
87
    expect(component.balanceControl.valid).toBe(true)
88
  })
89

90
  it('should hold balance returned by backend API', () => {
91
    walletService.get.and.returnValue(of(100))
92
    component.ngOnInit()
93
    fixture.detectChanges()
94
    expect(component.balance).toBe('100.00')
95
  })
96

97
  it('should log error while getting balance from backend API directly to browser console', fakeAsync(() => {
98
    walletService.get.and.returnValue(throwError('Error'))
99
    console.log = jasmine.createSpy('log')
100
    component.ngOnInit()
101
    expect(console.log).toHaveBeenCalledWith('Error')
102
  }))
103
})
104

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.