Skip to content

Commit

Permalink
fix: (CXSPA-939) - Buttons should be triggered by space and enter
Browse files Browse the repository at this point in the history
  • Loading branch information
sdrozdsap committed Apr 18, 2024
1 parent f593eac commit 34a06eb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class="btn-link"
role="button"
[attr.aria-label]="country.name + '(' + country.count + ')'"
(keydown.space)="navigateToLocation(country.isoCode, $event)"
>
<h2 class="cx-title" aria-hidden="true">
<span
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,51 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { I18nTestingModule } from '@spartacus/core';
import { I18nTestingModule, RoutingService } from '@spartacus/core';
import { StoreFinderService } from '@spartacus/storefinder/core';
import { SpinnerModule } from '@spartacus/storefront';
import { EMPTY } from 'rxjs';
import { of } from 'rxjs';
import { StoreFinderStoresCountComponent } from './store-finder-stores-count.component';
import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';
import { RouterTestingModule } from '@angular/router/testing';
import createSpy = jasmine.createSpy;

const mockLocation = {
isoCode: 'US',
name: 'United States',
count: 50,
};
class MockStoreFinderService implements Partial<StoreFinderService> {
viewAllStores = createSpy('viewAllStores');
getViewAllStoresEntities = createSpy(
'getViewAllStoresEntities'
).and.returnValue(EMPTY);
).and.returnValue(of([mockLocation]));
getViewAllStoresLoading = createSpy('getViewAllStoresLoading');
}

class MockRoutingService implements Partial<RoutingService> {
go = () => Promise.resolve(true);
}

describe('StoreFinderStoresCountComponent', () => {
let component: StoreFinderStoresCountComponent;
let fixture: ComponentFixture<StoreFinderStoresCountComponent>;
let el: DebugElement;
let routingService: RoutingService;

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [SpinnerModule, RouterTestingModule, I18nTestingModule],
imports: [SpinnerModule, I18nTestingModule, RouterTestingModule],
declarations: [StoreFinderStoresCountComponent],
providers: [
{
provide: StoreFinderService,
useClass: MockStoreFinderService,
},
{
provide: RoutingService,
useClass: MockRoutingService,
},
],
}).compileComponents();
})
Expand All @@ -37,10 +54,41 @@ describe('StoreFinderStoresCountComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(StoreFinderStoresCountComponent);
component = fixture.componentInstance;
el = fixture.debugElement;
routingService = TestBed.inject(RoutingService);
fixture.detectChanges();

spyOn(routingService, 'go').and.callThrough();
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('should navigate to country when navigateToLocation is called', () => {
component.navigateToLocation('US');
expect(routingService.go).toHaveBeenCalledWith([
'/store-finder/country',
'US',
]);
});

it('should handle space key to navigate to country from keyboard', () => {
spyOn(component, 'navigateToLocation').and.callThrough();
const countryBtn = el.query(
By.css('.btn-link[aria-label="United States(50)"]')
);
expect(countryBtn).toBeTruthy();
const event = new KeyboardEvent('keydown', {
key: ' ',
});
spyOn(event, 'preventDefault');
countryBtn.nativeElement.dispatchEvent(event);
expect(component.navigateToLocation).toHaveBeenCalledWith('US', event);
expect(routingService.go).toHaveBeenCalledWith([
'/store-finder/country',
'US',
]);
expect(event.preventDefault).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { StoreFinderService } from '@spartacus/storefinder/core';
import { RoutingService } from '@spartacus/core';

@Component({
selector: 'cx-store-finder-stores-count',
Expand All @@ -16,11 +17,19 @@ export class StoreFinderStoresCountComponent implements OnInit {
locations$: Observable<any>;
isLoading$: Observable<boolean>;

constructor(private storeFinderService: StoreFinderService) {}
constructor(
private storeFinderService: StoreFinderService,
private routingService: RoutingService
) {}

ngOnInit() {
this.storeFinderService.viewAllStores();
this.locations$ = this.storeFinderService.getViewAllStoresEntities();
this.isLoading$ = this.storeFinderService.getViewAllStoresLoading();
}

navigateToLocation(isoCode: string, event?: Event): void {
event?.preventDefault();
this.routingService.go(['/store-finder/country', isoCode]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
<button
[attr.aria-label]="'common.reset' | cxTranslate"
[title]="'common.reset' | cxTranslate"
(mousedown)="clear(searchInput)"
(keydown.enter)="clear(searchInput)"
(click)="clear(searchInput)"
class="reset"
>
<cx-icon [type]="iconTypes.RESET"></cx-icon>
Expand Down

0 comments on commit 34a06eb

Please sign in to comment.