From 9496cdde02130823dca3edc32a58181c6b17991a Mon Sep 17 00:00:00 2001 From: LauraPinilla Date: Fri, 26 Jul 2024 15:25:21 -0600 Subject: [PATCH] fixing tests --- .../_children/FacebookSignIn.jsx | 28 ++- .../social-sign-on/_children/GoogleSignIn.jsx | 52 +++-- .../components/social-sign-on/index.test.jsx | 209 ++++++++---------- .../features/signup/default.test.jsx | 1 - 4 files changed, 132 insertions(+), 158 deletions(-) diff --git a/blocks/identity-block/components/social-sign-on/_children/FacebookSignIn.jsx b/blocks/identity-block/components/social-sign-on/_children/FacebookSignIn.jsx index 8a765a9c1..2dc559d97 100644 --- a/blocks/identity-block/components/social-sign-on/_children/FacebookSignIn.jsx +++ b/blocks/identity-block/components/social-sign-on/_children/FacebookSignIn.jsx @@ -12,21 +12,19 @@ function FacebookSignIn({ customButtons, socialSignOnIn, className }) { if (customButtons) { return ( -
- -
+ ); } diff --git a/blocks/identity-block/components/social-sign-on/_children/GoogleSignIn.jsx b/blocks/identity-block/components/social-sign-on/_children/GoogleSignIn.jsx index 1e30c2b55..159eb2e5f 100644 --- a/blocks/identity-block/components/social-sign-on/_children/GoogleSignIn.jsx +++ b/blocks/identity-block/components/social-sign-on/_children/GoogleSignIn.jsx @@ -12,33 +12,31 @@ function GoogleSignIn({ customButtons, socialSignOnIn, className }) { if (customButtons) { return ( -
- -
+ ); } return ( diff --git a/blocks/identity-block/components/social-sign-on/index.test.jsx b/blocks/identity-block/components/social-sign-on/index.test.jsx index eceb9bbba..9451a0d1d 100644 --- a/blocks/identity-block/components/social-sign-on/index.test.jsx +++ b/blocks/identity-block/components/social-sign-on/index.test.jsx @@ -1,11 +1,18 @@ import React from "react"; import { render, screen } from "@testing-library/react"; -import '@testing-library/jest-dom'; +import "@testing-library/jest-dom"; import { useIdentity, usePhrases } from "@wpmedia/arc-themes-components"; import SocialSignOn from "./index"; import { GoogleSignInProvider } from "./utils/googleContext"; -jest.mock("@wpmedia/arc-themes-components"); +const mockButton = jest.fn(); + +jest.mock('@wpmedia/arc-themes-components', () => ({ + ...jest.requireActual('@wpmedia/arc-themes-components'), + Button: (props) => mockButton(props), + useIdentity: jest.fn(), + usePhrases: jest.fn() + })); describe("Identity Social Login Component", () => { it("renders nothing if config settings are false", () => { @@ -34,84 +41,79 @@ describe("Identity Social Login Component", () => { , ); // don't render any button - expect(screen.queryByTestId('fb-login-button')).not.toBeInTheDocument(); - expect(screen.queryByTestId('google-sign-in-button')).not.toBeInTheDocument(); - expect(screen.queryByTestId('apple-btn')).not.toBeInTheDocument(); + expect(screen.queryByTestId("fb-login-button")).not.toBeInTheDocument(); + expect(screen.queryByTestId("google-sign-in-button")).not.toBeInTheDocument(); + expect(screen.queryByTestId("apple-btn")).not.toBeInTheDocument(); }); - it("renders only Google (default) button", () => { + it("renders", () => { useIdentity.mockImplementation(() => ({ isInitialized: true, - isLoggedIn: () => false, + isLoggedIn: () => true, Identity: { configOptions: { googleClientId: true, - facebookAppId: false, + facebookAppId: true, }, + getConfig: jest.fn(() => + Promise.resolve({ + signinRecaptcha: false, + recaptchaSiteKey: "6LdXKVQcAAAAAO2tv3GdUbSK-1vcgujX6cP0IgF_", + }), + ), initFacebookLogin: () => {}, initializeFacebook: () => {}, + isLoggedIn: jest.fn(() => false), }, })); - usePhrases.mockImplementation(() => ({ - t: jest - .fn() - .mockReturnValue( - "Sign-in prompt was suppressed by the user or dismissed. Please try again later or use another sign-in method.", - ), - })); render( - null} redirectURL="#" customButtons={false}/> + null} redirectURL="#" /> , ); - expect(screen.getByTestId('google-sign-in-button')).toBeInTheDocument(); - expect(screen.queryByTestId('fb-login-button')).not.toBeInTheDocument(); + expect(screen.getByTestId("fb-login-button")).toBeInTheDocument(); + expect(screen.getByTestId("google-sign-in-button")).toBeInTheDocument(); }); - it("renders only Google (custom) button", () => { + it("calls getConfig if the options are missing", () => { + const getConfigMock = jest.fn(() => Promise.resolve()); useIdentity.mockImplementation(() => ({ - isInitialized: true, - isLoggedIn: () => false, Identity: { - configOptions: { - googleClientId: true, - facebookAppId: false, - }, + getConfig: getConfigMock, initFacebookLogin: () => {}, initializeFacebook: () => {}, + isLoggedIn: jest.fn(() => false), }, - })); - usePhrases.mockImplementation(() => ({ - t: jest - .fn() - .mockReturnValue( - "Sign-in prompt was suppressed by the user or dismissed. Please try again later or use another sign-in method.", - ), + isInitialized: true, + isLoggedIn: () => true, })); render( - null} redirectURL="#" customButtons /> + null} redirectURL="#" /> , ); - expect(screen.getByTestId('custom-google-sign-in-button')).toBeInTheDocument(); - expect(screen.queryByTestId('fb-login-button')).not.toBeInTheDocument(); + expect(getConfigMock).toHaveBeenCalled(); }); - it("renders only Facebook (default) button", () => { + it("calls facebookSignOn", () => { + const facebookSignOnMock = jest.fn(() => Promise.resolve()); useIdentity.mockImplementation(() => ({ isInitialized: true, - isLoggedIn: () => false, + isLoggedIn: () => true, Identity: { configOptions: { - googleClientId: false, + googleClientId: true, facebookAppId: true, }, + facebookSignOn: facebookSignOnMock, + getConfig: () => {}, initFacebookLogin: () => {}, initializeFacebook: () => {}, + isLoggedIn: jest.fn(() => false), }, })); @@ -120,149 +122,126 @@ describe("Identity Social Login Component", () => { null} redirectURL="#" /> , ); - - expect(screen.getByTestId('fb-login-button')).toBeInTheDocument(); - expect(screen.queryByTestId('google-sign-in-button')).not.toBeInTheDocument(); + window.onFacebookSignOn(); + expect(facebookSignOnMock).toHaveBeenCalled(); }); - it("renders only Facebook (custom) button", () => { + it("calls onError when login fails", () => { + const facebookSignOnMock = jest.fn(() => { + throw new Error(); + }); + const onErrorMock = jest.fn(); useIdentity.mockImplementation(() => ({ isInitialized: true, - isLoggedIn: () => false, + isLoggedIn: () => true, Identity: { configOptions: { - googleClientId: false, + googleClientId: true, facebookAppId: true, }, + facebookSignOn: facebookSignOnMock, + getConfig: () => {}, initFacebookLogin: () => {}, initializeFacebook: () => {}, + isLoggedIn: jest.fn(() => false), }, })); + usePhrases.mockImplementation(() => ({ + t: jest + .fn() + .mockReturnValue( + "Sign-in prompt was suppressed by the user or dismissed. Please try again later or use another sign-in method.", + ), + })); render( - null} redirectURL="#" customButtons /> + , ); - - expect(screen.getByTestId('custom-facebook-btn')).toBeInTheDocument(); - expect(screen.queryByTestId('google-sign-in-button')).not.toBeInTheDocument(); + window.onFacebookSignOn(); + expect(onErrorMock).toHaveBeenCalled(); }); +}); - it("renders", () => { +describe("Identity Social Login Component - Google Button", () => { + beforeEach(() => { useIdentity.mockImplementation(() => ({ isInitialized: true, - isLoggedIn: () => true, + isLoggedIn: () => false, Identity: { configOptions: { googleClientId: true, - facebookAppId: true, + facebookAppId: false, }, - getConfig: jest.fn(() => - Promise.resolve({ - signinRecaptcha: false, - recaptchaSiteKey: "6LdXKVQcAAAAAO2tv3GdUbSK-1vcgujX6cP0IgF_", - }), - ), initFacebookLogin: () => {}, initializeFacebook: () => {}, - isLoggedIn: jest.fn(() => false), }, })); - + }); + it("renders only Google (default) button", () => { render( - null} redirectURL="#" /> + null} redirectURL="#" customButtons={false} /> , ); - expect(screen.getByTestId('fb-login-button')).toBeInTheDocument(); - expect(screen.getByTestId('google-sign-in-button')).toBeInTheDocument(); + expect(screen.getByTestId("google-sign-in-button")).toBeInTheDocument(); + expect(screen.queryByTestId("fb-login-button")).not.toBeInTheDocument(); }); - it("calls getConfig if the options are missing", () => { - const getConfigMock = jest.fn(() => Promise.resolve()); - useIdentity.mockImplementation(() => ({ - Identity: { - getConfig: getConfigMock, - initFacebookLogin: () => {}, - initializeFacebook: () => {}, - isLoggedIn: jest.fn(() => false), - }, - isInitialized: true, - isLoggedIn: () => true, - })); + it("renders only Google (custom) button", () => { + mockButton.mockImplementation(() =>
); render( - null} redirectURL="#" /> + null} redirectURL="#" customButtons /> , ); - expect(getConfigMock).toHaveBeenCalled(); + expect(screen.getByTestId("custom-google-sign-in-button")).toBeInTheDocument(); + expect(screen.queryByTestId("fb-login-button")).not.toBeInTheDocument(); }); +}); - it("calls facebookSignOn", () => { - const facebookSignOnMock = jest.fn(() => Promise.resolve()); +describe("Identity Social Login Component - Facebook Button", () => { + + beforeEach(() => { useIdentity.mockImplementation(() => ({ isInitialized: true, - isLoggedIn: () => true, + isLoggedIn: () => false, Identity: { configOptions: { - googleClientId: true, + googleClientId: false, facebookAppId: true, }, - facebookSignOn: facebookSignOnMock, - getConfig: () => {}, initFacebookLogin: () => {}, initializeFacebook: () => {}, - isLoggedIn: jest.fn(() => false), }, })); + }); + it("renders only Facebook (default) button", () => { render( null} redirectURL="#" /> , ); - window.onFacebookSignOn(); - expect(facebookSignOnMock).toHaveBeenCalled(); + + expect(screen.getByTestId("fb-login-button")).toBeInTheDocument(); + expect(screen.queryByTestId("google-sign-in-button")).not.toBeInTheDocument(); }); - it("calls onError when login fails", () => { - const facebookSignOnMock = jest.fn(() => { - throw new Error(); - }); - const onErrorMock = jest.fn(); - useIdentity.mockImplementation(() => ({ - isInitialized: true, - isLoggedIn: () => true, - Identity: { - configOptions: { - googleClientId: true, - facebookAppId: true, - }, - facebookSignOn: facebookSignOnMock, - getConfig: () => {}, - initFacebookLogin: () => {}, - initializeFacebook: () => {}, - isLoggedIn: jest.fn(() => false), - }, - })); - usePhrases.mockImplementation(() => ({ - t: jest - .fn() - .mockReturnValue( - "Sign-in prompt was suppressed by the user or dismissed. Please try again later or use another sign-in method.", - ), - })); + it("renders only Facebook (custom) button", () => { + mockButton.mockImplementation(() =>
); render( - + null} redirectURL="#" customButtons /> , ); - window.onFacebookSignOn(); - expect(onErrorMock).toHaveBeenCalled(); + + expect(screen.getByTestId("custom-facebook-btn")).toBeInTheDocument(); + expect(screen.queryByTestId("google-sign-in-button")).not.toBeInTheDocument(); }); }); diff --git a/blocks/identity-block/features/signup/default.test.jsx b/blocks/identity-block/features/signup/default.test.jsx index 5b90ef5dd..36760c8f2 100644 --- a/blocks/identity-block/features/signup/default.test.jsx +++ b/blocks/identity-block/features/signup/default.test.jsx @@ -47,7 +47,6 @@ jest.mock("@wpmedia/arc-themes-components", () => ({ ...mockSales, }, })), - // Button: ({onClick}) => , BotChallengeProtection: ({ challengeIn= 'signup' }) =>
}));