-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathheader.component.spec.ts
More file actions
137 lines (114 loc) · 4.27 KB
/
Copy pathheader.component.spec.ts
File metadata and controls
137 lines (114 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { HeaderComponent } from './header.component'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { RouterTestingModule } from '@angular/router/testing'
import { MatDialog } from '@angular/material/dialog'
import { A11yModule } from '@angular/cdk/a11y'
import { MatButtonModule } from '@angular/material/button'
import { WINDOW_PROVIDERS } from '../../cdk/window'
import { PlatformInfoService } from '../../cdk/platform-info'
import { ErrorHandlerService } from '../../core/error-handler/error-handler.service'
import { SnackbarService } from '../../cdk/snackbar/snackbar.service'
import { MatSnackBar } from '@angular/material/snack-bar'
import { Overlay } from '@angular/cdk/overlay'
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'
import { PlatformInfo } from '../../cdk/platform-info'
describe('HeaderComponent', () => {
let component: HeaderComponent
let fixture: ComponentFixture<HeaderComponent>
const mobilePlatform = {
columns12: false,
columns8: false,
columns4: true,
} as unknown as PlatformInfo
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
A11yModule,
HttpClientTestingModule,
MatButtonModule,
RouterTestingModule,
],
declarations: [HeaderComponent],
providers: [
WINDOW_PROVIDERS,
PlatformInfoService,
ErrorHandlerService,
SnackbarService,
MatSnackBar,
MatDialog,
Overlay,
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents()
})
beforeEach(() => {
fixture = TestBed.createComponent(HeaderComponent)
component = fixture.componentInstance
fixture.detectChanges()
})
it('should create', () => {
expect(component).toBeTruthy()
})
it('hides the mobile search while the menu is open', () => {
component.platform = mobilePlatform
component.mobileMenuState = false
component.isCompactActive = false
fixture.detectChanges()
expect(fixture.nativeElement.querySelector('app-search')).toBeTruthy()
component.mobileMenuState = true
fixture.detectChanges()
expect(fixture.nativeElement.querySelector('app-search')).toBeFalsy()
})
it('renders public nav items without a sign-in/register action', () => {
component.platform = mobilePlatform
component.mobileMenuState = true
component.user = undefined
fixture.detectChanges()
const navButtons = fixture.nativeElement.querySelectorAll(
'nav button'
) as NodeListOf<HTMLButtonElement>
const buttonLabels = Array.from(navButtons).map(
(button: HTMLButtonElement) =>
button.textContent?.replace(/\s+/g, ' ').trim()
)
expect(buttonLabels[0]).toBe('ABOUT')
expect(
buttonLabels.some((label) => label?.includes('Sign in'))
).toBe(false)
})
it('sets active menu item id when a navigable item is clicked', () => {
component.platform = {
columns12: true,
} as unknown as PlatformInfo
const item = component.menu[0]
spyOn(component, 'goto')
component.click([], item)
expect(component.activeMenuItemId).toBe(item.id)
expect(component.goto).toHaveBeenCalledWith(item.route)
})
it('locks and restores body scroll while the mobile menu is open', () => {
document.body.style.overflow = 'auto'
component.setMobileMenuOpen(true)
expect(document.body.style.overflow).toBe('hidden')
component.setMobileMenuOpen(false)
expect(document.body.style.overflow).toBe('auto')
})
it('applies the mobile overlay class when the mobile menu is open', () => {
component.platform = mobilePlatform
component.hideMainMenu = false
component.mobileMenuState = true
fixture.detectChanges()
const nav = fixture.nativeElement.querySelector('nav')
expect(nav?.classList.contains('mobile-nav-overlay')).toBe(true)
expect(nav?.getAttribute('aria-modal')).toBe('true')
})
it('reports menu item active when route matches or item was clicked', () => {
const item = component.menu[0]
component.currentRoute = '/' + item.route
expect(component.isMenuItemActive(item)).toBe(true)
component.currentRoute = '/other'
component.activeMenuItemId = item.id
expect(component.isMenuItemActive(item)).toBe(true)
})
})