-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
29 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 28 additions & 28 deletions
56
packages/helpers/tests/i18nCreateInterpolateElementTest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
import React from 'react'; | ||
import React from "react"; | ||
import { i18nCreateInterpolateElement } from "../src/i18n-create-interpolate-element"; | ||
import { createInterpolateElement } from "@wordpress/element"; | ||
import { sprintf } from "@wordpress/i18n"; | ||
|
||
jest.mock("@wordpress/element", () => ({ | ||
jest.mock( "@wordpress/element", () => ( { | ||
createInterpolateElement: jest.fn(), | ||
})); | ||
} ) ); | ||
|
||
jest.mock("@wordpress/i18n", () => ({ | ||
jest.mock( "@wordpress/i18n", () => ( { | ||
sprintf: jest.fn(), | ||
})); | ||
} ) ); | ||
|
||
describe("i18nCreateInterpolateElement", () => { | ||
beforeEach(() => { | ||
describe( "i18nCreateInterpolateElement", () => { | ||
beforeEach( () => { | ||
jest.clearAllMocks(); | ||
}); | ||
} ); | ||
|
||
it("should return interpolated element when no error occurs", () => { | ||
it( "should return interpolated element when no error occurs", () => { | ||
const translatedString = "Hello %s"; | ||
const args = ["world"]; | ||
const args = [ "world" ]; | ||
const conversionMap = { world: <span>world</span> }; | ||
const interpolatedElement = <span>Hello world</span>; | ||
|
||
sprintf.mockReturnValue("Hello world"); | ||
createInterpolateElement.mockReturnValue(interpolatedElement); | ||
sprintf.mockReturnValue( "Hello world" ); | ||
createInterpolateElement.mockReturnValue( interpolatedElement ); | ||
|
||
const result = i18nCreateInterpolateElement(translatedString, args, conversionMap); | ||
const result = i18nCreateInterpolateElement( translatedString, args, conversionMap ); | ||
|
||
expect(sprintf).toHaveBeenCalledWith(translatedString, ...args); | ||
expect(createInterpolateElement).toHaveBeenCalledWith("Hello world", conversionMap); | ||
expect(result).toBe(interpolatedElement); | ||
}); | ||
expect( sprintf ).toHaveBeenCalledWith( translatedString, ...args ); | ||
expect( createInterpolateElement ).toHaveBeenCalledWith( "Hello world", conversionMap ); | ||
expect( result ).toBe( interpolatedElement ); | ||
} ); | ||
|
||
it("should return translated string and log error when an error occurs", () => { | ||
it( "should return translated string and log error when an error occurs", () => { | ||
const translatedString = "Hello %s"; | ||
const args = ["world"]; | ||
const args = [ "world" ]; | ||
const conversionMap = { world: <span>world</span> }; | ||
|
||
sprintf.mockImplementation(() => { | ||
throw new Error("Test error"); | ||
}); | ||
sprintf.mockImplementation( () => { | ||
throw new Error( "Test error" ); | ||
} ); | ||
|
||
console.error = jest.fn(); | ||
|
||
const result = i18nCreateInterpolateElement(translatedString, args, conversionMap); | ||
const result = i18nCreateInterpolateElement( translatedString, args, conversionMap ); | ||
|
||
expect(sprintf).toHaveBeenCalledWith(translatedString, ...args); | ||
expect(console.error).toHaveBeenCalledWith("Error in translation for:", translatedString); | ||
expect(result).toBe(translatedString); | ||
}); | ||
}); | ||
expect( sprintf ).toHaveBeenCalledWith( translatedString, ...args ); | ||
expect( console.error ).toHaveBeenCalledWith( "Error in translation for:", translatedString ); | ||
expect( result ).toBe( translatedString ); | ||
} ); | ||
} ); |