-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: automatic submits in authenticate component (#124)
* fix: instant submits when no validator is set * chore: add missing translation in demo component * test: bump coverage Co-authored-by: Arne Vandoorslaer <[email protected]>
- Loading branch information
Showing
4 changed files
with
121 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/dgt-components/lib/components/authentication/authenticate.machine.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { createMachine, interpret, Interpreter, StateMachine } from 'xstate'; | ||
import { AuthenticateContext, AuthenticateEvent, authenticateMachine, AuthenticateState, AuthenticateStates, AuthenticateStateSchema, ClickedLoginEvent, WebIdEnteredEvent } from './authenticate.machine'; | ||
|
||
describe('AuthenticateMachine', () => { | ||
|
||
let actor: Interpreter<AuthenticateContext, AuthenticateStateSchema, AuthenticateEvent, AuthenticateState>; | ||
let machine: StateMachine<AuthenticateContext, AuthenticateStateSchema, AuthenticateEvent, AuthenticateState>; | ||
|
||
const solidService = { | ||
getSession: jest.fn(async () => { throw new Error(); }), // mock failing of session restore | ||
getIssuers: jest.fn(async () => []), | ||
} as any; | ||
|
||
beforeEach(() => { | ||
|
||
machine = createMachine(authenticateMachine(solidService)).withContext({}); | ||
actor = interpret(machine); | ||
|
||
}); | ||
|
||
describe('CHECKING_ISSUERS', () => { | ||
|
||
beforeEach(async () => { | ||
|
||
// go to CHECKING_ISSUERS state | ||
actor.onTransition((state) => { | ||
|
||
if (state.matches(AuthenticateStates.AWAITING_WEBID)) { | ||
|
||
// go to AWAITING_LOGIN state | ||
actor.send(new WebIdEnteredEvent('https://example.com/profile/card#me')); | ||
|
||
} else if (state.matches(AuthenticateStates.AWAITING_LOGIN)) { | ||
|
||
// go to RETRIEVING_ISSUERS state | ||
actor.send(new ClickedLoginEvent('https://example.com/profile/card#me')); | ||
|
||
} | ||
|
||
}); | ||
|
||
}); | ||
|
||
it('should transition to NO_TRUST when issuers is empty', async () => { | ||
|
||
solidService.getIssuers = jest.fn(async () => []); | ||
|
||
actor.onTransition((state) => { | ||
|
||
if (state.matches(AuthenticateStates.NO_TRUST)) { | ||
|
||
expect(state.context.issuers.length).toEqual(0); | ||
|
||
} | ||
|
||
}); | ||
|
||
actor.start(); | ||
|
||
}); | ||
|
||
it('should transition to AUTHENTICATING when single issuer', async () => { | ||
|
||
solidService.getIssuers = jest.fn(async () => [ 'https://issuer.uri/' ]); | ||
|
||
actor.onTransition((state) => { | ||
|
||
if (state.matches(AuthenticateStates.AUTHENTICATING)) { | ||
|
||
expect(state.context.issuers.length).toEqual(1); | ||
expect(state.context.issuer).toEqual('https://issuer.uri/'); | ||
|
||
} | ||
|
||
}); | ||
|
||
actor.start(); | ||
|
||
}); | ||
|
||
it('should transition to SELECTING_ISSUER when multiple issuers', async () => { | ||
|
||
solidService.getIssuers = jest.fn(async () => [ 'https://issuer1.uri/', 'https://issuer2.uri/' ]); | ||
|
||
actor.onTransition((state) => { | ||
|
||
if (state.matches(AuthenticateStates.SELECTING_ISSUER)) { | ||
|
||
expect(state.context.issuers.length).toBeGreaterThan(1); | ||
|
||
} | ||
|
||
}); | ||
|
||
actor.start(); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters