fix: restore pre-existing App auth tests dropped in EnvBanner wiring
This commit is contained in:
@@ -1,27 +1,87 @@
|
||||
import { signal } from '@angular/core';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { provideHttpClient } from '@angular/common/http';
|
||||
import { provideHttpClientTesting } from '@angular/common/http/testing';
|
||||
import { provideRouter } from '@angular/router';
|
||||
import { of } from 'rxjs';
|
||||
import { App } from './app';
|
||||
import { AuthApi } from './core/auth/auth-api';
|
||||
import { AuthStore } from './core/auth/auth-store';
|
||||
import { environment } from '../environments/environment';
|
||||
|
||||
describe('App', () => {
|
||||
const originalProduction = environment.production;
|
||||
const token = signal<string | null>(null);
|
||||
const updateUser = vi.fn();
|
||||
const meResponse = signal<Record<string, unknown>>({
|
||||
id: 1,
|
||||
email: 'a@b.de',
|
||||
firstName: 'Alex',
|
||||
lastName: 'Muster',
|
||||
});
|
||||
const me = vi.fn(() => of(meResponse()));
|
||||
const setSession = vi.fn();
|
||||
|
||||
beforeEach(() => {
|
||||
localStorage.clear();
|
||||
beforeEach(async () => {
|
||||
token.set(null);
|
||||
meResponse.set({ id: 1, email: 'a@b.de', firstName: 'Alex', lastName: 'Muster' });
|
||||
updateUser.mockClear();
|
||||
setSession.mockClear();
|
||||
me.mockClear();
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [App],
|
||||
providers: [
|
||||
provideRouter([]),
|
||||
{ provide: AuthStore, useValue: { token, updateUser, setSession } },
|
||||
{ provide: AuthApi, useValue: { me } },
|
||||
],
|
||||
}).compileComponents();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
environment.production = originalProduction;
|
||||
});
|
||||
|
||||
it('sets --env-banner-height to 0px and renders no banner in production', async () => {
|
||||
it('should create the app', () => {
|
||||
const fixture = TestBed.createComponent(App);
|
||||
expect(fixture.componentInstance).toBeTruthy();
|
||||
});
|
||||
|
||||
it('validates and refreshes a restored session on startup', () => {
|
||||
token.set('stored-token');
|
||||
|
||||
TestBed.createComponent(App);
|
||||
|
||||
expect(me).toHaveBeenCalled();
|
||||
expect(updateUser).toHaveBeenCalledWith({
|
||||
id: 1,
|
||||
email: 'a@b.de',
|
||||
firstName: 'Alex',
|
||||
lastName: 'Muster',
|
||||
});
|
||||
});
|
||||
|
||||
it('replaces an expired stored token when me returns a refreshed token', () => {
|
||||
token.set('expired-token');
|
||||
meResponse.set({
|
||||
id: 1,
|
||||
email: 'a@b.de',
|
||||
firstName: 'Alex',
|
||||
lastName: 'Muster',
|
||||
token: 'refreshed-token',
|
||||
});
|
||||
|
||||
TestBed.createComponent(App);
|
||||
|
||||
expect(setSession).toHaveBeenCalledWith('refreshed-token', {
|
||||
id: 1,
|
||||
email: 'a@b.de',
|
||||
firstName: 'Alex',
|
||||
lastName: 'Muster',
|
||||
});
|
||||
expect(updateUser).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('sets --env-banner-height to 0px and renders no banner in production', () => {
|
||||
environment.production = true;
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [App],
|
||||
providers: [provideHttpClient(), provideHttpClientTesting(), provideRouter([])],
|
||||
}).compileComponents();
|
||||
const fixture = TestBed.createComponent(App);
|
||||
fixture.detectChanges();
|
||||
|
||||
@@ -29,12 +89,8 @@ describe('App', () => {
|
||||
expect(fixture.nativeElement.querySelector('.env-banner')).toBeNull();
|
||||
});
|
||||
|
||||
it('sets --env-banner-height to 28px and renders the banner outside production', async () => {
|
||||
it('sets --env-banner-height to 28px and renders the banner outside production', () => {
|
||||
environment.production = false;
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [App],
|
||||
providers: [provideHttpClient(), provideHttpClientTesting(), provideRouter([])],
|
||||
}).compileComponents();
|
||||
const fixture = TestBed.createComponent(App);
|
||||
fixture.detectChanges();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user