diff --git a/myteamwallet_frontend_modern/src/app/app.spec.ts b/myteamwallet_frontend_modern/src/app/app.spec.ts index ab9aa98..d81b160 100644 --- a/myteamwallet_frontend_modern/src/app/app.spec.ts +++ b/myteamwallet_frontend_modern/src/app/app.spec.ts @@ -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(null); + const updateUser = vi.fn(); + const meResponse = signal>({ + 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();