Skip to content

Commit

Permalink
fix(vue): remove mock facade values code (#4061)
Browse files Browse the repository at this point in the history
* Remove dependency on mock facade

* Update comment

* Remove //@ts-ignore

* useAuthenticatorValue cleanup

* Update packages/vue/src/composables/useAuth.ts

* Update packages/vue/src/composables/useAuth.ts

* Update packages/vue/src/composables/useAuth.ts

* Update tests

* fix typo

* Update packages/vue/src/composables/__tests__/useAuthenticator.spec.ts
  • Loading branch information
wlee221 committed Jul 14, 2023
1 parent fc591e8 commit d56317e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 46 deletions.
11 changes: 9 additions & 2 deletions packages/vue/src/composables/__tests__/useAuthenticator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MockAuthService {

const mockServiceFacade = {
authStatus: 'authenticated',
socialProviders: ['amazon'],
} as unknown as AuthenticatorServiceFacade;

const getServiceFacadeSpy = jest
Expand Down Expand Up @@ -54,13 +55,19 @@ describe('useAuthenticator', () => {
it('returns the expected values', () => {
const wrapper = mount(TestComponent);

expect(wrapper.vm.authStatus).toBe('unauthenticated');
expect(wrapper.vm.socialProviders).toStrictEqual(['amazon']);
wrapper.unmount();
});

it('calls getServiceFacade once on initial mount', () => {
const wrapper = mount(TestComponent);

expect(getServiceFacadeSpy).toBeCalledTimes(1);
expect(getServiceFacadeSpy).toBeCalledWith({
send: mockSend,
state: mockState.value,
});

expect(wrapper.vm.authStatus).toBe('unauthenticated');
wrapper.unmount();
});

Expand Down
26 changes: 15 additions & 11 deletions packages/vue/src/composables/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from '@aws-amplify/ui';

import { UseAuth } from '../types';
import { facade } from './useUtils';

export const useAuth = createSharedComposable((): UseAuth => {
const machine = createAuthenticatorMachine();
Expand Down Expand Up @@ -53,20 +52,25 @@ export const useAuth = createSharedComposable((): UseAuth => {
export const useAuthenticator = createSharedComposable(() => {
const { authStatus, state, send } = useAuth();

const useAuthenticatorValue = reactive({
...facade,
send,
state,
}) as any;
// TODO(BREAKING): remove the cast to any
const useAuthenticatorValue = reactive({}) as any;

/*
* Note that watchEffect runs immediately, so `useAuthenticatorValue` is
* guaranteed to have facade values by the time `useAuthenticator` returns.
*
* https://vuejs.org/api/reactivity-core.html#watcheffect
*/
watchEffect(() => {
const facadeValues = getServiceFacade({
send,
state: state.value,
});
const facade = getServiceFacade({ send, state: state.value });

/*
* TODO(BREAKING): consider using a plain object with `refs` instead of
* one `reactive` object to prevent iterating manually over its keys.
*/
for (const key of Object.keys(facade)) {
//@ts-ignore
useAuthenticatorValue[key] = facadeValues[key];
useAuthenticatorValue[key] = facade[key];
}
useAuthenticatorValue.authStatus = authStatus.value;
useAuthenticatorValue.send = send;
Expand Down
33 changes: 0 additions & 33 deletions packages/vue/src/composables/useUtils.ts

This file was deleted.

0 comments on commit d56317e

Please sign in to comment.