Skip to content

Commit

Permalink
fix: fix social login actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jpklzm authored and ruifcnunes committed Sep 13, 2023
1 parent c0d1c5b commit 88ead15
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { cleanup, renderHook } from '@testing-library/react';
import { mockInitialState } from 'tests/__fixtures__/authentication/index.mjs';
import {
mockInitialState,
mockResponse,
} from 'tests/__fixtures__/authentication/index.mjs';
import { mockStore } from '../../../../tests/helpers/index.js';
import { postAccountLink, postSocialLogin } from '@farfetch/blackout-client';
import { Provider } from 'react-redux';
Expand All @@ -24,15 +27,15 @@ jest.mock('@farfetch/blackout-client', () => {

return {
...original,
postSocialLogin: jest.fn(() => Promise.resolve()),
postAccountLink: jest.fn(() => Promise.resolve()),
postSocialLogin: jest.fn(() => Promise.resolve(mockResponse)),
postAccountLink: jest.fn(() => Promise.resolve(mockResponse)),
};
});

const genericMock = {
actions: {
createAccountLink: expect.any(Function),
login: expect.any(Function),
socialLogin: expect.any(Function),
},
};

Expand Down Expand Up @@ -61,19 +64,19 @@ describe('useSocialLogin', () => {
});

describe('actions', () => {
describe('login', () => {
it('should call `useSocialLogin` login action with config', async () => {
describe('socialLogin', () => {
it('should call `useSocialLogin` socialLogin action with config', async () => {
const current = getRenderedHook(mockInitialState);

await current.actions.login(mockLoginData, mockConfig);
await current.actions.socialLogin(mockLoginData, mockConfig);

expect(postSocialLogin).toHaveBeenCalledWith(mockLoginData, mockConfig);
});

it('should call `useSocialLogin` login action without config', async () => {
it('should call `useSocialLogin` socialLogin action without config', async () => {
const current = getRenderedHook(mockInitialState);

await current.actions.login(mockLoginData);
await current.actions.socialLogin(mockLoginData);

expect(postSocialLogin).toHaveBeenCalledWith(mockLoginData, undefined);
});
Expand Down
12 changes: 6 additions & 6 deletions packages/react/src/authentication/hooks/useSocialLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import {
} from '@farfetch/blackout-client';
import {
createAccountLink as createAccountLinkAction,
socialLogin as socialLoginAction,
socialLogin as socialLoginReduxAction,
} from '@farfetch/blackout-redux';
import { useCallback } from 'react';
import useAction from '../../helpers/useAction.js';

function useSocialLogin() {
const socialLogin = useAction(socialLoginAction);
const socialLoginAction = useAction(socialLoginReduxAction);
const accountLink = useAction(createAccountLinkAction);
const login = useCallback(
const socialLogin = useCallback(
async (data: PostSocialLoginData, config?: Config) => {
if (!data) {
return Promise.reject(new Error('No data was specified.'));
}

return await socialLogin(data, config);
return await socialLoginAction(data, config);
},
[socialLogin],
[socialLoginAction],
);

const createAccountLink = useCallback(
Expand All @@ -37,7 +37,7 @@ function useSocialLogin() {

return {
actions: {
login,
socialLogin,
createAccountLink,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Object {
"entities": Object {
"user": Object {},
},
"result": undefined,
},
"type": "@farfetch/blackout-redux/CREATE_ACCOUNT_LINK_SUCCESS",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Object {
"wishlistId": "1e5232f8-7af0-4fba-b6b1-b87e2cb3a88f",
},
},
"result": 29556478,
},
"type": "@farfetch/blackout-redux/SOCIAL_LOGIN_SUCCESS",
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as actionTypes from '../../actionTypes.js';
import {
expectedNormalizedSocialLoginPayload,
expectedNormalizedPayload,
mockResponse,
} from 'tests/__fixtures__/authentication/index.mjs';
import { find } from 'lodash-es';
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('socialLogin() action creator', () => {
{ type: actionTypes.SOCIAL_LOGIN_REQUEST },
{
type: actionTypes.SOCIAL_LOGIN_SUCCESS,
payload: expectedNormalizedSocialLoginPayload,
payload: expectedNormalizedPayload,
},
]);
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const createAccountLinkFactory =
const user = result;
const userEntity = {
entities: { user },
result: result.id,
};

dispatch({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const socialLoginFactory =

const userEntity = {
entities: { user },
result: result.id,
};

await dispatch({
Expand Down

0 comments on commit 88ead15

Please sign in to comment.