Skip to content

Commit

Permalink
Adapt unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FAMarfuaty committed Sep 4, 2024
1 parent fb5b18e commit 2645991
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const expectedResults = {
wordComplexity: {
isApplicable: true,
score: 6,
resultText: "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: 25.16% of the words in your text are considered complex. " +
resultText: "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: Some words in your text are considered complex. " +
"<a href='https://yoa.st/4lt' target='_blank'>Try to use shorter and more familiar words to improve readability</a>.",
},
textAlignment: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const expectedResults = {
wordComplexity: {
isApplicable: true,
score: 6,
resultText: "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: 25.52% of the words in your text are considered complex." +
resultText: "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: Some words in your text are considered complex." +
" <a href='https://yoa.st/4lt' target='_blank'>Try to use shorter and more familiar words to improve readability</a>.",
},
textAlignment: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sprintf, _n } from "@wordpress/i18n";
import Mark from "../../../../src/values/Mark";
import Paper from "../../../../src/values/Paper.js";
import EnglishResearcher from "../../../../src/languageProcessing/languages/en/Researcher";
Expand Down Expand Up @@ -61,7 +62,7 @@ describe( "tests assessment results in languages written from left to right (LTR

expect( assessmentResult.getScore() ).toBe( 2 );
expect( assessmentResult.getText() ).toBe( "<a href='https://yoa.st/assessment-alignment' target='_blank'>Alignment</a>:" +
" There are 2 long sections of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>" +
" There are long sections of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>" +
"We recommend making them left-aligned</a>." );
expect( assessmentResult.hasMarks() ).toBe( true );
expect( textAlignmentAssessment.getMarks( mockPaperLTR, researcher ) ).toEqual( [
Expand All @@ -86,7 +87,7 @@ describe( "tests assessment results in languages written from left to right (LTR

expect( assessmentResult.getScore() ).toBe( 2 );
expect( assessmentResult.getText() ).toBe( "<a href='https://yoa.st/assessment-alignment' target='_blank'>Alignment</a>:" +
" There are 2 long sections of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>" +
" There are long sections of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>" +
"We recommend making them left-aligned</a>." );
expect( assessmentResult.hasMarks() ).toBe( true );
expect( textAlignmentAssessment.getMarks( mockPaperLTR, researcher ) ).toEqual( [
Expand All @@ -111,7 +112,7 @@ describe( "tests assessment results in languages written from left to right (LTR

expect( assessmentResult.getScore() ).toBe( 2 );
expect( assessmentResult.getText() ).toBe( "<a href='https://yoa.st/assessment-alignment' target='_blank'>Alignment</a>:" +
" There are 2 long sections of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>" +
" There are long sections of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>" +
"We recommend making them left-aligned</a>." );
expect( assessmentResult.hasMarks() ).toBe( true );
expect( textAlignmentAssessment.getMarks( mockPaperLTR, researcher ) ).toEqual( [
Expand Down Expand Up @@ -190,7 +191,7 @@ describe( "tests the feedback strings of the assessment run for languages writte

expect( assessmentResult.getScore() ).toBe( 2 );
expect( assessmentResult.getText() ).toBe( "<a href='https://yoa.st/assessment-alignment' target='_blank'>Alignment</a>: " +
"There are 2 long sections of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>" +
"There are long sections of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>" +
"We recommend making them right-aligned</a>." );
expect( assessmentResult.hasMarks() ).toBe( true );
expect( textAlignmentAssessment.getMarks( mockPaperRTL, researcher ) ).toEqual( [
Expand All @@ -208,6 +209,84 @@ describe( "tests the feedback strings of the assessment run for languages writte
} );
} );

describe( "tests for retrieving the feedback strings.", function() {
it( "returns the default strings when no custom `getResultTexts` callback is provided.", function() {
const assessment = new TextAlignmentAssessment();
expect( assessment.getFeedbackStrings() ).toEqual( {
leftToRight: "<a href='https://yoa.st/assessment-alignment' target='_blank'>Alignment</a>: There are long sections of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>We recommend making them left-aligned</a>.",
rightToLeft: "<a href='https://yoa.st/assessment-alignment' target='_blank'>Alignment</a>: There are long sections of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>We recommend making them right-aligned</a>.",
} );
} );
it( "returns the default strings when no custom `getResultTexts` callback is provided: there is one long section of center-aligned text", function() {
const assessment = new TextAlignmentAssessment();
const mockPaperLTR = new Paper( "<p class=\"has-text-align-center\">This is a paragraph with a bit more than fifty characters.</p>" );
const mockResearcher = new EnglishResearcher( mockPaperLTR );
mockResearcher.addResearch( "getLongCenterAlignedTexts", getLongCenterAlignedTexts );

assessment.getResult( mockPaperLTR, mockResearcher );

expect( assessment.getFeedbackStrings() ).toEqual( {
leftToRight: "<a href='https://yoa.st/assessment-alignment' target='_blank'>Alignment</a>: There is a long section of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>We recommend making it left-aligned</a>.",
rightToLeft: "<a href='https://yoa.st/assessment-alignment' target='_blank'>Alignment</a>: There is a long section of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>We recommend making it right-aligned</a>.",
} );
} );
it( "overrides the default strings when a custom `getResultTexts` callback is provided", function() {
/**
* Returns the result texts for the Text alignment assessment.
* @param {Object} config The configuration object that contains data needed to generate the result text.
* @param {string} config.urlTitleAnchorOpeningTag The anchor opening tag to the article about this assessment.
* @param {string} config.urlActionAnchorOpeningTag The anchor opening tag to the call to action URL to the help article of this assessment.
* @param {number} config.numberOfLongCenterAlignedTexts The number of long center-aligned texts found in the text.
*
* @returns {{rightToLeft: string, leftToRight: string}} The object that contains the result texts as a translation string.
*/
const getResultTexts = ( { urlTitleAnchorOpeningTag, urlActionAnchorOpeningTag, numberOfLongCenterAlignedTexts } ) => {
return {
rightToLeft: sprintf(
/* translators: %1$s and %2$s expand to links on yoast.com, %3$s expands to the anchor end tag,
%4$s expands to the number of the long center-aligned sections in the text */
_n(
"%1$sAlignment%3$s: There is a long section of center-aligned text. %2$sWe recommend making it right-aligned%3$s.",
"%1$sAlignment%3$s: There are %4$s long sections of center-aligned text. %2$sWe recommend making them right-aligned%3$s.",
numberOfLongCenterAlignedTexts,
"wordpress-seo-premium"
),
urlTitleAnchorOpeningTag,
urlActionAnchorOpeningTag,
"</a>",
numberOfLongCenterAlignedTexts
),
leftToRight: sprintf(
/* translators: %1$s and %2$s expand to links on yoast.com, %3$s expands to the anchor end tag,
%4$s expands to the number of the long center-aligned sections in the text */
_n(
"%1$sAlignment%3$s: There is a long section of center-aligned text. %2$sWe recommend making it left-aligned%3$s.",
"%1$sAlignment%3$s: There are %4$s long sections of center-aligned text. %2$sWe recommend making them left-aligned%3$s.",
numberOfLongCenterAlignedTexts,
"wordpress-seo-premium"
),
urlTitleAnchorOpeningTag,
urlActionAnchorOpeningTag,
"</a>",
numberOfLongCenterAlignedTexts
),
};
};
const assessment = new TextAlignmentAssessment( { callbacks: { getResultTexts } } );
const mockPaperLTR = new Paper( "<p class=\"has-text-align-center\">This is a paragraph with a bit more than fifty characters.</p>" +
"<p class=\"has-text-align-center\">This is another paragraph with a bit more than fifty characters.</p>" );
const mockResearcher = new EnglishResearcher( mockPaperLTR );
mockResearcher.addResearch( "getLongCenterAlignedTexts", getLongCenterAlignedTexts );

assessment.getResult( mockPaperLTR, mockResearcher );

expect( assessment.getFeedbackStrings() ).toEqual( {
leftToRight: "<a href='https://yoa.st/assessment-alignment' target='_blank'>Alignment</a>: There are 2 long sections of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>We recommend making them left-aligned</a>.",
rightToLeft: "<a href='https://yoa.st/assessment-alignment' target='_blank'>Alignment</a>: There are 2 long sections of center-aligned text. <a href='https://yoa.st/assessment-alignment-cta' target='_blank'>We recommend making them right-aligned</a>.",
} );
} );
} );

describe( "tests for the assessment's applicability.", function() {
it( "returns false when the paper is empty.", function() {
const paperWithNoText = new Paper( "" );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sprintf, __ } from "@wordpress/i18n";
import wordComplexityConfigEnglish from "../../../../src/languageProcessing/languages/en/config/wordComplexity";
import EnglishResearcher from "../../../../src/languageProcessing/languages/en/Researcher";
import DefaultResearcher from "../../../../src/languageProcessing/languages/_default/Researcher";
Expand Down Expand Up @@ -70,7 +71,7 @@ describe( "a test for an assessment that checks complex words in a text", functi
const result = assessment.getResult( runningPaper, researcher );

expect( result.getScore() ).toBe( 6 );
expect( result.getText() ).toBe( "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: 10.11% of the words in " +
expect( result.getText() ).toBe( "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: Some words in " +
"your text are considered complex. <a href='https://yoa.st/4lt' target='_blank'>Try to use shorter and more familiar words " +
"to improve readability</a>." );
expect( result.hasMarks() ).toBe( true );
Expand All @@ -87,7 +88,7 @@ describe( "a test for an assessment that checks complex words in a text", functi
const result = assessment.getResult( runningPaper, researcher );

expect( result.getScore() ).toBe( 3 );
expect( result.getText() ).toBe( "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: 10.11% of the words in " +
expect( result.getText() ).toBe( "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: Some words in " +
"your text are considered complex. <a href='https://yoa.st/4lt' target='_blank'>Try to use shorter and more familiar words " +
"to improve readability</a>." );
expect( result.hasMarks() ).toBe( true );
Expand All @@ -105,7 +106,7 @@ describe( "a test for an assessment that checks complex words in a text", functi
const result = assessment.getResult( runningPaper, researcher );

expect( result.getScore() ).toBe( 3 );
expect( result.getText() ).toBe( "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: 10% of the words in " +
expect( result.getText() ).toBe( "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: Some words in " +
"your text are considered complex. <a href='https://yoa.st/4lt' target='_blank'>Try to use shorter and more familiar words " +
"to improve readability</a>." );
expect( result.hasMarks() ).toBe( true );
Expand Down Expand Up @@ -190,3 +191,81 @@ describe( "tests for the assessment applicability", function() {
} );
} );

describe( "tests for retrieving the feedback strings.", function() {
let researcher;

beforeEach( () => {
researcher = new EnglishResearcher();
researcher.addResearch( "wordComplexity", wordComplexity );
researcher.addHelper( "checkIfWordIsComplex", wordComplexityHelperEnglish );
researcher.addConfig( "wordComplexity", wordComplexityConfigEnglish );
researcher.addResearchData( "morphology", morphologyData );
} );

it( "returns the default strings when no custom `getResultTexts` callback is provided.", function() {
const wordComplexityAssessment = new WordComplexityAssessment();
expect( wordComplexityAssessment.getFeedbackStrings() ).toEqual( {
acceptableAmount: "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: Some words in your text are considered complex. <a href='https://yoa.st/4lt' target='_blank'>Try to use shorter and more familiar words to improve readability</a>.",
goodAmount: "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: You are not using too many complex words, which makes your text easy to read. Good job!",
} );
} );
const runningPaper = new Paper( "Also called torties for short, tortoiseshell cats combine two colors other than white, " +
"either closely mixed or in larger patches." +
" The colors are often described as red and black, but the \"red\" patches can instead be orange, yellow, or cream," +
" and the \"black\" can instead be chocolate, gray, tabby, or blue. Tortoiseshell cats with the tabby pattern as one of their colors " +
"are sometimes referred to as a torbie. Cats having torbie coats are sometimes referred to as torbie cats.\n" +
"\"Tortoiseshell\" is typically reserved for particolored cats with relatively small or no white markings. " +
"Those that are predominantly white with tortoiseshell patches are described as tricolor, tortoiseshell-and-white " +
"(in the United Kingdom), or calico (in Canada and the United States).\n" +
"Cats with tortoiseshell pattern and small blotches of white are sometimes referred to as \"tortico\" by their owners, " +
"a portmanteau of \"tortie\" and \"calico\"\n" +
"Torbie cats with a predominantly white undercoat are often referred to as \"caliby\" by their respective owners, " +
"an amalgamation of Calico and Tabby." +
"Torbie cats with a predominantly white undercoat are often referred to as \"caliby\" by their respective owners, " +
"an amalgamation of Calico and Tabby." );
it( "overrides the default feedback strings when custom `getResultTexts` callback is provided.", function() {
/**
* Returns the result texts for the Word Complexity assessment.
* @param {Object} config The configuration object that contains data needed to generate the result text.
* @param {string} config.urlTitleAnchorOpeningTag The anchor opening tag to the article about this assessment.
* @param {string} config.urlActionAnchorOpeningTag The anchor opening tag to the call to action URL to the help article of this assessment.
* @param {number} config.complexWordsPercentage The percentage of complex words found in the text.
* @returns {{acceptableAmount: string, goodAmount: string}} The object that contains the result texts as a translation string.
*/
const getResultTexts = ( { urlTitleAnchorOpeningTag, urlActionAnchorOpeningTag, complexWordsPercentage } ) => {
return {
acceptableAmount: sprintf(
/* translators: %1$s expands to a link on yoast.com, %2$s expand to the percentage of the complex words found in the text.
%3$s expand to a link on yoast.com, %4$s expands to the anchor end tag. */
__(
"%1$sWord complexity%4$s: %2$s of the words in your text are considered complex. %3$sTry to use shorter and more familiar words to improve readability%4$s.",
"wordpress-seo-premium"
),
urlTitleAnchorOpeningTag,
complexWordsPercentage + "%",
urlActionAnchorOpeningTag,
"</a>"
),
goodAmount: sprintf(
/* translators: %1$s expands to an article on yoast.com and %4$s expands to the anchor end tag. */
__(
"%1$sWord complexity%2$s: You are not using too many complex words, which makes your text easy to read. Good job!",
"wordpress-seo-premium"
),
urlTitleAnchorOpeningTag,
"</a>"
),
};
};

const wordComplexityAssessment = new WordComplexityAssessment( { callbacks: { getResultTexts } } );
researcher.setPaper( runningPaper );

wordComplexityAssessment.getResult( runningPaper, researcher );

expect( wordComplexityAssessment.getFeedbackStrings() ).toEqual( {
acceptableAmount: "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: 10.11% of the words in your text are considered complex. <a href='https://yoa.st/4lt' target='_blank'>Try to use shorter and more familiar words to improve readability</a>.",
goodAmount: "<a href='https://yoa.st/4ls' target='_blank'>Word complexity</a>: You are not using too many complex words, which makes your text easy to read. Good job!",
} );
} );
} );
Loading

0 comments on commit 2645991

Please sign in to comment.