Skip to content

Commit

Permalink
Add tests for the modal content
Browse files Browse the repository at this point in the history
  • Loading branch information
vraja-pro committed Dec 13, 2024
1 parent 64984f8 commit 9809a34
Show file tree
Hide file tree
Showing 2 changed files with 4,414 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,94 @@
import * as SEMrushRelatedKeyphrasesModalContent from "../../src/components/SEMrushRelatedKeyphrasesModalContent";
import RelatedKeyphraseModalContent, { hasError, getUserMessage, hasMaximumRelatedKeyphrases } from "../../src/components/SEMrushRelatedKeyphrasesModalContent";
import { render } from "../test-utils";
import { expect } from "@jest/globals";

const succesfulResponse = {
results: {
columnNames: [ "Keyword", "Search Volume", "Trends", "Keyword Difficulty Index", "Intent" ],
rows: [
[
"speed test",
"13600000",
"0.44,1.00,0.44,0.44,0.44,0.24,0.24,0.36,0.44,0.44,0.44,0.44",
"9",
"0",
],
[
"internet speed test",
"7480000",
"0.82,0.0",
"50",
"0,2",
],
[
"automated test",
"1500000",
"0.20,0.24,0.24,0.16,0.36,0.29,0.66,0.81,0.36,0.36,0.20,0.16",
"90",
"3",
],
[
"test",
"1500000",
"0.36,0.19,0.29,0.16,0.16,0.16,0.16,0.29,0.16,0.19,0.16,0.19",
"30",
"0,2,3,1",
],
[
"wifi speed test",
"823000",
"1.00,0.44,0.44,0.44,0.54,0.44,0.44,0.54,0.54,0.54,0.54,0.54",
"15",
"0,2",
],
[
"typing test",
"550000",
"0.55,0.55,0.55,0.45,0.36,0.55,0.55,0.55,0.45,0.36,0.36,0.55",
"95",
"0,3",
],
[
"accesability test",
"301000",
"0.36,0.44,0.44,0.44,0.44,0.54,0.44,0.54,0.36,0.44,0.44,0.29",
"80",
"0",
],
[
"seo test",
"301000",
"0.66,0.54,0.54,0.54,0.66,0.54,0.66,0.66,0.54,0.44,0.44,0.54",
"86",
"3",
],
[
"related keyphrase test",
"246000",
"0.66,0.66,0.54,0.81,1.00,0.81,0.81,0.81,0.66,0.54,0.54,0.54",
"60",
"1,3",
],
[
"storybook tests",
"246000",
"0.81,0.54,0.44,0.54,0.44,0.13,0.20,0.04,0.06,0.07,0.20,0.29",
"72",
"1",
],
],
},
status: 200,
};

jest.mock( "@wordpress/api-fetch", () => {
return {
__esModule: true,
"default": jest.fn( () => {
return Promise.resolve( succesfulResponse );
} ),
};
} );

describe( "SEMrushRelatedKeyphrasesModalContent", () => {
let props = {};
Expand All @@ -25,13 +115,13 @@ describe( "SEMrushRelatedKeyphrasesModalContent", () => {

describe( "hasError", () => {
it( "returns that the response has no error property", () => {
const actual = SEMrushRelatedKeyphrasesModalContent.hasError( { status: 200 } );
const actual = hasError( { status: 200 } );

expect( actual ).toBe( false );
} );

it( "returns that the limit has been reached", () => {
const actual = SEMrushRelatedKeyphrasesModalContent.hasError( { error: "An error!", status: 500 } );
const actual = hasError( { error: "An error!", status: 500 } );

expect( actual ).toBe( true );
} );
Expand All @@ -44,7 +134,7 @@ describe( "SEMrushRelatedKeyphrasesModalContent", () => {
requestLimitReached: true,
};

const actual = SEMrushRelatedKeyphrasesModalContent.getUserMessage( props );
const actual = getUserMessage( props );

expect( actual ).toEqual( "requestLimitReached" );
} );
Expand All @@ -59,27 +149,27 @@ describe( "SEMrushRelatedKeyphrasesModalContent", () => {
},
};

const actual = SEMrushRelatedKeyphrasesModalContent.getUserMessage( props );
const actual = getUserMessage( props );

expect( actual ).toEqual( "requestFailed" );
} );

it( "returns a message when response contains no data", () => {
const actual = SEMrushRelatedKeyphrasesModalContent.getUserMessage( props );
const actual = getUserMessage( props );

expect( actual ).toEqual( "requestEmpty" );
} );
} );

describe( "hasMaximumRelatedKeyphrases", () => {
it( "returns that maximum related keyphrases hasn't been reached when there are none", () => {
const actual = SEMrushRelatedKeyphrasesModalContent.hasMaximumRelatedKeyphrases( [] );
const actual = hasMaximumRelatedKeyphrases( [] );

expect( actual ).toBe( false );
} );

it( "returns that maximum related keyphrases hasn't been reached when there is less than 4", () => {
const actual = SEMrushRelatedKeyphrasesModalContent.hasMaximumRelatedKeyphrases( [
const actual = hasMaximumRelatedKeyphrases( [
{ key: "a", keyword: "yoast seo", score: 33 },
{ key: "b", keyword: "yoast seo plugin", score: 33 },
{ key: "c", keyword: "yoast plugin", score: 33 },
Expand All @@ -89,7 +179,7 @@ describe( "SEMrushRelatedKeyphrasesModalContent", () => {
} );

it( "returns that the limit has been reached", () => {
const actual = SEMrushRelatedKeyphrasesModalContent.hasMaximumRelatedKeyphrases( [
const actual = hasMaximumRelatedKeyphrases( [
{ key: "a", keyword: "yoast seo", score: 33 },
{ key: "b", keyword: "yoast seo plugin", score: 33 },
{ key: "c", keyword: "yoast plugin", score: 33 },
Expand All @@ -99,4 +189,94 @@ describe( "SEMrushRelatedKeyphrasesModalContent", () => {
expect( actual ).toBe( true );
} );
} );

describe( "RelatedKeyphraseModalContent", () => {
it( "renders the SEMrush related keyphrases modal content with results and without premium", async() => {
props = {
...props,
isPremium: false,
renderAction: null,
requestHasData: true,
response: succesfulResponse,
premiumUpsellLink: "https://yoa.st/413",
};

const { container } = render( <RelatedKeyphraseModalContent { ...props } /> );
expect( container ).toMatchSnapshot();
} );

it( "renders the SEMrush related keyphrases modal content with results and with premium", async() => {
props = {
...props,
isPremium: true,
renderAction: jest.fn( () => <button>Add</button> ),
requestHasData: true,
response: succesfulResponse,
};

const { container } = render( <RelatedKeyphraseModalContent { ...props } /> );
expect( container ).toMatchSnapshot();
} );

it( "renders the SEMrush related keyphrases modal content with no results alert", async() => {
props = {
...props,
isPremium: true,
renderAction: jest.fn( () => <button>Add</button> ),
requestHasData: false,
response: {},
};

const { container } = render( <RelatedKeyphraseModalContent { ...props } /> );
expect( container ).toMatchSnapshot();
} );

it( "renders the SEMrush related keyphrases modal content with request limit reached alert", async() => {
props = {
...props,
isPremium: true,
renderAction: jest.fn( () => <button>Add</button> ),
requestLimitReached: true,
response: {},
semrushUpsellLink: "https://yoa.st/semrush-prices",
};

const { container } = render( <RelatedKeyphraseModalContent { ...props } /> );
expect( container ).toMatchSnapshot();
} );

it( "renders the SEMrush related keyphrases modal content with request failed alert", async() => {
props = {
...props,
isPremium: true,
renderAction: jest.fn( () => <button>Add</button> ),
isSuccess: false,
response: {
error: "An error!",
status: 500,
},
};

const { container } = render( <RelatedKeyphraseModalContent { ...props } /> );
expect( container ).toMatchSnapshot();
} );

it( "renders the SEMrush related keyphrases modal content with maximum related keyphrases alert", async() => {
props = {
...props,
isPremium: true,
renderAction: jest.fn( () => <button>Add</button> ),
relatedKeyphrases: [
{ key: "a", keyword: "yoast seo", score: 33 },
{ key: "b", keyword: "yoast seo plugin", score: 33 },
{ key: "c", keyword: "yoast plugin", score: 33 },
{ key: "d", keyword: "yoast premium plugin", score: 33 },
],
requestHasData: true,
};

const { container } = render( <RelatedKeyphraseModalContent { ...props } /> );
expect( container ).toMatchSnapshot();
} );
} );
} );
Loading

0 comments on commit 9809a34

Please sign in to comment.