Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vue): remove mock facade values code #4061

Merged
merged 12 commits into from
Jun 9, 2023
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'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this because socialProviders was previously broken.

} 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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This initial value is not needed, because watchEffect below is guaranteed to run immediately (see comment below). And ...facade was spreading a mock value anyway, so we aren't losing anything.

I actually prefer this because now we only have one place where we populate useAuthenticatorValue.


/*
* 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.