99import '@testing-library/jest-dom' ;
1010import { Auth0Provider , useAuth0 } from '..' ;
1111import Auth0 from '../../index' ;
12- import { Auth0User } from '../../core/models' ;
1312
1413// Mock TurboModuleRegistry first
1514jest . mock ( 'react-native/Libraries/TurboModule/TurboModuleRegistry' , ( ) => ( {
@@ -68,8 +67,12 @@ jest.mock('../../index');
6867const MockAuth0 = Auth0 as jest . MockedClass < typeof Auth0 > ;
6968
7069// Mock the Auth0User model's factory method
71- jest . mock ( '../../core/models/Auth0User' ) ;
72- const MockAuth0User = Auth0User as jest . MockedClass < typeof Auth0User > ;
70+ jest . mock ( '../../core/models/Auth0User' , ( ) => ( {
71+ Auth0User : {
72+ fromIdToken : jest . fn ( ) ,
73+ } ,
74+ } ) ) ;
75+ const { Auth0User : MockAuth0User } = require ( '../../core/models/Auth0User' ) ;
7376
7477// 2. A more complete mock client factory
7578const createMockClient = ( ) => {
@@ -89,6 +92,7 @@ const createMockClient = () => {
8992 cancelWebAuth : jest . fn ( ) . mockResolvedValue ( undefined ) ,
9093 handleRedirectCallback : jest . fn ( ) . mockResolvedValue ( undefined ) ,
9194 checkWebSession : jest . fn ( ) . mockResolvedValue ( null ) ,
95+ getWebUser : jest . fn ( ) . mockResolvedValue ( null ) ,
9296 } ,
9397 credentialsManager : {
9498 hasValidCredentials : jest . fn ( ) . mockResolvedValue ( false ) ,
@@ -803,4 +807,70 @@ describe('Auth0Provider', () => {
803807 } ) ;
804808 } ) ;
805809 } ) ;
810+
811+ // Web Platform Method Tests
812+ describe ( 'Web Platform Methods' , ( ) => {
813+ it ( 'should verify webAuth methods exist and are callable' , ( ) => {
814+ // Verify all required webAuth methods exist in the mock
815+ expect ( mockClientInstance . webAuth . handleRedirectCallback ) . toBeDefined ( ) ;
816+ expect ( mockClientInstance . webAuth . getWebUser ) . toBeDefined ( ) ;
817+ expect ( mockClientInstance . webAuth . checkWebSession ) . toBeDefined ( ) ;
818+
819+ // Verify they are functions
820+ expect ( typeof mockClientInstance . webAuth . handleRedirectCallback ) . toBe (
821+ 'function'
822+ ) ;
823+ expect ( typeof mockClientInstance . webAuth . getWebUser ) . toBe ( 'function' ) ;
824+ expect ( typeof mockClientInstance . webAuth . checkWebSession ) . toBe (
825+ 'function'
826+ ) ;
827+ } ) ;
828+
829+ it ( 'should verify webAuth methods can be mocked properly' , async ( ) => {
830+ const mockUser = { sub : 'test|123' , name : 'Test User' } ;
831+
832+ // Setup mocks
833+ mockClientInstance . webAuth . handleRedirectCallback . mockResolvedValue (
834+ undefined
835+ ) ;
836+ mockClientInstance . webAuth . getWebUser . mockResolvedValue ( mockUser ) ;
837+ mockClientInstance . webAuth . checkWebSession . mockResolvedValue ( undefined ) ;
838+
839+ // Call the methods
840+ await mockClientInstance . webAuth . handleRedirectCallback ( ) ;
841+ const user = await mockClientInstance . webAuth . getWebUser ( ) ;
842+ await mockClientInstance . webAuth . checkWebSession ( ) ;
843+
844+ // Verify calls were made
845+ expect (
846+ mockClientInstance . webAuth . handleRedirectCallback
847+ ) . toHaveBeenCalledTimes ( 1 ) ;
848+ expect ( mockClientInstance . webAuth . getWebUser ) . toHaveBeenCalledTimes ( 1 ) ;
849+ expect ( mockClientInstance . webAuth . checkWebSession ) . toHaveBeenCalledTimes (
850+ 1
851+ ) ;
852+ expect ( user ) . toEqual ( mockUser ) ;
853+ } ) ;
854+
855+ it ( 'should verify the sequence of webAuth method calls can be tracked' , async ( ) => {
856+ // Setup mocks
857+ mockClientInstance . webAuth . handleRedirectCallback . mockResolvedValue (
858+ undefined
859+ ) ;
860+ mockClientInstance . webAuth . getWebUser . mockResolvedValue ( null ) ;
861+
862+ // Call methods in sequence
863+ await mockClientInstance . webAuth . handleRedirectCallback ( ) ;
864+ await mockClientInstance . webAuth . getWebUser ( ) ;
865+
866+ // Verify call order using invocationCallOrder
867+ const handleRedirectCallOrder =
868+ mockClientInstance . webAuth . handleRedirectCallback . mock
869+ . invocationCallOrder [ 0 ] ;
870+ const getWebUserCallOrder =
871+ mockClientInstance . webAuth . getWebUser . mock . invocationCallOrder [ 0 ] ;
872+
873+ expect ( getWebUserCallOrder ) . toBeGreaterThan ( handleRedirectCallOrder ) ;
874+ } ) ;
875+ } ) ;
806876} ) ;
0 commit comments