|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { FusionAuthService } from './FusionAuthService'; |
| 3 | +import { SDKConfig } from '@fusionauth-sdk/core'; |
| 4 | + |
| 5 | +describe('FusionAuthService', () => { |
| 6 | + const config: SDKConfig = { |
| 7 | + clientId: 'test-client-id', |
| 8 | + redirectUri: 'http://localhost:3000', |
| 9 | + serverUrl: 'http://localhost:9011', |
| 10 | + }; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + localStorage.clear(); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + localStorage.clear(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should configure the FusionAuthService and store config in localStorage', () => { |
| 21 | + const service = FusionAuthService.configure(config); |
| 22 | + const storedConfig = localStorage.getItem('fusionauth-config'); |
| 23 | + expect(storedConfig).toBe(JSON.stringify(config)); |
| 24 | + expect(service).toBeInstanceOf(FusionAuthService); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should retrieve the config from localStorage', () => { |
| 28 | + localStorage.setItem('fusionauth-config', JSON.stringify(config)); |
| 29 | + const retrievedConfig = FusionAuthService.getConfig(); |
| 30 | + expect(retrievedConfig).toEqual(config); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should throw an error if FusionAuthService is not configured', () => { |
| 34 | + const service = new FusionAuthService(config); |
| 35 | + localStorage.clear(); |
| 36 | + expect(() => service.startLogin()).toThrowError('FusionAuthService is not configured.'); |
| 37 | + expect(() => service.startRegister()).toThrowError('FusionAuthService is not configured.'); |
| 38 | + expect(() => service.startLogout()).toThrowError('FusionAuthService is not configured.'); |
| 39 | + expect(() => service.manageAccount()).toThrowError('FusionAuthService is not configured.'); |
| 40 | + expect(() => service.fetchUserInfo()).toThrowError('FusionAuthService is not configured.'); |
| 41 | + expect(() => service.refreshToken()).toThrowError('FusionAuthService is not configured.'); |
| 42 | + expect(() => service.initAutoRefresh()).toThrowError('FusionAuthService is not configured.'); |
| 43 | + expect(() => service.handlePostRedirect()).toThrowError('FusionAuthService is not configured.'); |
| 44 | + expect(() => service.isLoggedIn).toThrowError('FusionAuthService is not configured.'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should start login flow', () => { |
| 48 | + const service = FusionAuthService.configure(config); |
| 49 | + const startLoginSpy = vi.spyOn(service, 'startLogin'); |
| 50 | + service.startLogin(); |
| 51 | + expect(startLoginSpy).toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should start register flow', () => { |
| 55 | + const service = FusionAuthService.configure(config); |
| 56 | + const startRegisterSpy = vi.spyOn(service, 'startRegister'); |
| 57 | + service.startRegister(); |
| 58 | + expect(startRegisterSpy).toHaveBeenCalled(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should start logout flow', () => { |
| 62 | + const service = FusionAuthService.configure(config); |
| 63 | + const startLogoutSpy = vi.spyOn(service, 'startLogout'); |
| 64 | + service.startLogout(); |
| 65 | + expect(startLogoutSpy).toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should manage account', () => { |
| 69 | + const service = FusionAuthService.configure(config); |
| 70 | + const manageAccountSpy = vi.spyOn(service, 'manageAccount'); |
| 71 | + service.manageAccount(); |
| 72 | + expect(manageAccountSpy).toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should fetch user info', async () => { |
| 76 | + const service = FusionAuthService.configure(config); |
| 77 | + const fetchUserInfoSpy = vi.spyOn(service, 'fetchUserInfo'); |
| 78 | + await service.fetchUserInfo(); |
| 79 | + expect(fetchUserInfoSpy).toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should refresh token', async () => { |
| 83 | + const service = FusionAuthService.configure(config); |
| 84 | + const refreshTokenSpy = vi.spyOn(service, 'refreshToken'); |
| 85 | + await service.refreshToken(); |
| 86 | + expect(refreshTokenSpy).toHaveBeenCalled(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should initialize auto refresh', () => { |
| 90 | + const service = FusionAuthService.configure(config); |
| 91 | + const initAutoRefreshSpy = vi.spyOn(service, 'initAutoRefresh'); |
| 92 | + service.initAutoRefresh(); |
| 93 | + expect(initAutoRefreshSpy).toHaveBeenCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should handle post redirect', () => { |
| 97 | + const service = FusionAuthService.configure(config); |
| 98 | + const handlePostRedirectSpy = vi.spyOn(service, 'handlePostRedirect'); |
| 99 | + service.handlePostRedirect(); |
| 100 | + expect(handlePostRedirectSpy).toHaveBeenCalled(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should return isLoggedIn status', () => { |
| 104 | + const service = FusionAuthService.configure(config); |
| 105 | + const isLoggedInSpy = vi.spyOn(service, 'isLoggedIn', 'get'); |
| 106 | + const isLoggedIn = service.isLoggedIn; |
| 107 | + expect(isLoggedInSpy).toHaveBeenCalled(); |
| 108 | + expect(isLoggedIn).toBe(false); |
| 109 | + }); |
| 110 | +}); |
0 commit comments