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 { TestBed } from '@angular/core/testing';
|
||||||
import { provideHttpClient } from '@angular/common/http';
|
|
||||||
import { provideHttpClientTesting } from '@angular/common/http/testing';
|
|
||||||
import { provideRouter } from '@angular/router';
|
import { provideRouter } from '@angular/router';
|
||||||
|
import { of } from 'rxjs';
|
||||||
import { App } from './app';
|
import { App } from './app';
|
||||||
|
import { AuthApi } from './core/auth/auth-api';
|
||||||
|
import { AuthStore } from './core/auth/auth-store';
|
||||||
import { environment } from '../environments/environment';
|
import { environment } from '../environments/environment';
|
||||||
|
|
||||||
describe('App', () => {
|
describe('App', () => {
|
||||||
const originalProduction = environment.production;
|
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(() => {
|
beforeEach(async () => {
|
||||||
localStorage.clear();
|
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(() => {
|
afterEach(() => {
|
||||||
environment.production = originalProduction;
|
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;
|
environment.production = true;
|
||||||
await TestBed.configureTestingModule({
|
|
||||||
imports: [App],
|
|
||||||
providers: [provideHttpClient(), provideHttpClientTesting(), provideRouter([])],
|
|
||||||
}).compileComponents();
|
|
||||||
const fixture = TestBed.createComponent(App);
|
const fixture = TestBed.createComponent(App);
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
@@ -29,12 +89,8 @@ describe('App', () => {
|
|||||||
expect(fixture.nativeElement.querySelector('.env-banner')).toBeNull();
|
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;
|
environment.production = false;
|
||||||
await TestBed.configureTestingModule({
|
|
||||||
imports: [App],
|
|
||||||
providers: [provideHttpClient(), provideHttpClientTesting(), provideRouter([])],
|
|
||||||
}).compileComponents();
|
|
||||||
const fixture = TestBed.createComponent(App);
|
const fixture = TestBed.createComponent(App);
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user