You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
3 Hrs
13 Min
28 Seg

Pruebas de integraci贸n con Guardianes

18/20
Resources

Contributions 1

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Ahora haremos pruebas de integraci贸n con las funcionalidades de guardianes, ya que probaremos la integraci贸n de los guardianes desde una ruta a otra

primero cambiamos un poco el comportamiento del auth guard

auth.guard.ts

canActivate(
聽 聽 route: ActivatedRouteSnapshot,
聽 聽 state: RouterStateSnapshot): Observable<boolean> {
聽 聽 return this.authService.getUser()
聽 聽 .pipe(
聽 聽 聽 map(user => {
聽 聽 聽 聽 if(!user) {
聽 聽 聽 聽 聽 this.router.navigate(['/']);
聽 聽 聽 聽 聽 return false;
聽 聽 聽 聽 }
聽 聽 聽 聽 return true;
聽 聽 聽 })
聽 聽 )

Ingresamos el guardian en la ruta de others component

app-routing.module.ts

import { AuthGuard } from './guards/auth.guard';

export const routes: Routes = [
聽 {
聽 聽 path: 'others',
聽 聽 canActivate: [AuthGuard],
聽 聽 component: OthersComponent
聽 },
];

Ahora podemos hacer mockin de un usuario y probar si logra entrar o no con el guardian activo

app.integration.spec.ts

import { AuthService } from './services/auth.service';
import { generateOneUser } from './models/user.mock';

describe('App Integration test', () => {
聽 let authService: jasmine.SpyObj<AuthService>;

聽 beforeEach(async () => {
聽 聽 const authServiceSpy = jasmine.createSpyObj('AuthService', ['getUser']);

聽 聽 await TestBed.configureTestingModule({
聽 聽 聽 imports: [
聽 聽 聽 聽 AppModule,
聽 聽 聽 聽 RouterTestingModule.withRoutes(routes)
聽 聽 聽 ],
聽 聽 聽 providers: [
聽 聽 聽 聽 { provide: AuthService, useValue: authServiceSpy }
聽 聽 聽 ],
聽 聽 聽 schemas: [NO_ERRORS_SCHEMA]
聽 聽 }).compileComponents();
聽 });


it('should render OthersComponent when clicked with session', fakeAsync(() => {
聽 聽 const productsMocks = generateManyProducts(10);

聽 聽 productService.getAll.and.returnValue(asyncData(productsMocks));
聽 聽 const userMock = generateOneUser();
聽 聽 authService.getUser.and.returnValue(mockObservable(userMock));
聽 聽 clickElement(fixture, 'others-link', true);
聽 聽 tick(); // wait while nav...
聽 聽 fixture.detectChanges(); // ngOnInit - OthersComponent
聽 聽 tick();
聽 聽 fixture.detectChanges(); // ngOnInit - OthersComponent

聽 聽 expect(router.url).toEqual('/others');
聽 聽 const element = query(fixture, 'app-others');

聽 聽 expect(element).not.toBeNull();
聽 聽 const text = getText(fixture, 'products-length');
聽 聽 
聽 聽 expect(text).toContain(productsMocks.length.toString());
聽 }));
  

聽 it('should render OthersComponent when clicked without session', fakeAsync(() => {
聽 聽 authService.getUser.and.returnValue(mockObservable(null));
聽 聽 clickElement(fixture, 'others-link', true);
聽 聽 tick(); // wait while nav...
聽 聽 
聽 聽 fixture.detectChanges(); // ngOnInit - OthersComponent
聽 聽 tick();

聽 聽 fixture.detectChanges(); // ngOnInit - OthersComponent
聽 聽 expect(router.url).toEqual('/');
聽 }));