juice-shop

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

6
import { type ComponentFixture, fakeAsync, flush, TestBed, waitForAsync } from '@angular/core/testing'
7
import { TranslateModule } from '@ngx-translate/core'
8
import { ProductReviewEditComponent } from './product-review-edit.component'
9
import { ReactiveFormsModule } from '@angular/forms'
10
import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog'
11
import { MatFormFieldModule } from '@angular/material/form-field'
12
import { MatButtonModule } from '@angular/material/button'
13
import { of, throwError } from 'rxjs'
14
import { ProductReviewService } from 'src/app/Services/product-review.service'
15
import { MatInputModule } from '@angular/material/input'
16
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
17
import { MatSnackBarModule } from '@angular/material/snack-bar'
18

19
describe('ProductReviewEditComponent', () => {
20
  let component: ProductReviewEditComponent
21
  let fixture: ComponentFixture<ProductReviewEditComponent>
22
  let productReviewService: any
23
  let dialogRef: any
24

25
  beforeEach(waitForAsync(() => {
26
    productReviewService = jasmine.createSpyObj('ProductReviewService', ['patch'])
27
    productReviewService.patch.and.returnValue(of({}))
28
    dialogRef = jasmine.createSpyObj('MatDialogRef', ['close'])
29
    dialogRef.close.and.returnValue({})
30

31
    TestBed.configureTestingModule({
32
      imports: [
33
        TranslateModule.forRoot(),
34
        ReactiveFormsModule,
35
        BrowserAnimationsModule,
36
        MatDialogModule,
37
        MatFormFieldModule,
38
        MatInputModule,
39
        MatButtonModule,
40
        MatSnackBarModule
41
      ],
42
      declarations: [ProductReviewEditComponent],
43
      providers: [
44
        { provide: ProductReviewService, useValue: productReviewService },
45
        { provide: MAT_DIALOG_DATA, useValue: { productData: {} } },
46
        { provide: MatDialogRef, useValue: dialogRef }
47
      ]
48
    })
49
      .compileComponents()
50
  }))
51

52
  beforeEach(() => {
53
    fixture = TestBed.createComponent(ProductReviewEditComponent)
54
    component = fixture.componentInstance
55
    component.data = { reviewData: { _id: '42', message: 'Review', author: 'Horst' } }
56
    fixture.detectChanges()
57
  })
58

59
  it('should create', () => {
60
    expect(component).toBeTruthy()
61
  })
62

63
  it('should be initialized with data from the passed review', () => {
64
    component.data = { reviewData: { _id: '42', message: 'Review', author: 'Horst' } }
65
    component.ngOnInit()
66
    expect(component.editReviewControl.value).toBe('Review')
67
  })
68

69
  it('should update review through backend API', () => {
70
    component.data = { reviewData: { _id: '42', message: 'Review', author: 'Horst' } }
71
    component.ngOnInit()
72
    component.editReviewControl.setValue('Another Review')
73
    component.editReview()
74
    expect(productReviewService.patch.calls.count()).toBe(1)
75
    expect(productReviewService.patch.calls.argsFor(0)[0]).toEqual({ id: '42', message: 'Another Review' })
76
  })
77

78
  it('should close the dialog on submitting the edited review', () => {
79
    productReviewService.patch.and.returnValue(of({}))
80
    component.data = { reviewData: { _id: '42', message: 'Review', author: 'Horst' } }
81
    component.ngOnInit()
82
    component.editReview()
83
    expect(dialogRef.close).toHaveBeenCalled()
84
  })
85

86
  it('should log errors directly to browser console', fakeAsync(() => {
87
    component.data = { reviewData: { _id: '42', message: 'Review', author: 'Horst' } }
88
    console.log = jasmine.createSpy('log')
89
    productReviewService.patch.and.returnValue(throwError('Error'))
90
    component.ngOnInit()
91
    component.editReview()
92
    expect(console.log).toHaveBeenCalledWith('Error')
93
    fixture.destroy()
94
    flush()
95
  }))
96
})
97

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

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

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

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