template_0205
This commit is contained in:
75
RN_TEMPLATE/test/i18n.test.ts
Normal file
75
RN_TEMPLATE/test/i18n.test.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { exec } from "child_process"
|
||||
|
||||
import en from "../app/i18n/en"
|
||||
|
||||
// Use this array for keys that for whatever reason aren't greppable so they
|
||||
// don't hold your test suite hostage by always failing.
|
||||
const EXCEPTIONS: string[] = [
|
||||
// "welcomeScreen:readyForLaunch",
|
||||
|
||||
/**
|
||||
* This translation key actually shows up in a comment describing the usage of the translate
|
||||
* function in the app/i18n/translate.ts file. Because the grep command in the i18n test below
|
||||
* doesn't account for commented out code, we must manually exclude it so tests don't fail
|
||||
* because of a comment.
|
||||
*/
|
||||
"hello",
|
||||
]
|
||||
|
||||
function iterate(obj, stack, array) {
|
||||
for (const property in obj) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, property)) {
|
||||
if (typeof (obj as object)[property] === "object") {
|
||||
iterate(obj[property], `${stack}.${property}`, array)
|
||||
} else {
|
||||
array.push(`${stack.slice(1)}.${property}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array
|
||||
}
|
||||
|
||||
/**
|
||||
* This tests your codebase for missing i18n strings so you can avoid error strings at render time
|
||||
*
|
||||
* It was taken from https://gist.github.com/Michaelvilleneuve/8808ba2775536665d95b7577c9d8d5a1
|
||||
* and modified slightly to account for our Ignite higher order components,
|
||||
* which take 'tx' and 'fooTx' props.
|
||||
* The grep command is nasty looking, but it's essentially searching the codebase for a few different things:
|
||||
*
|
||||
* tx="*"
|
||||
* Tx=""
|
||||
* tx={""}
|
||||
* Tx={""}
|
||||
* translate(""
|
||||
*
|
||||
* and then grabs the i18n key between the double quotes
|
||||
*
|
||||
* This approach isn't 100% perfect. If you are storing your key string in a variable because you
|
||||
* are setting it conditionally, then it won't be picked up.
|
||||
*
|
||||
*/
|
||||
|
||||
describe("i18n", () => {
|
||||
test("There are no missing keys", (done) => {
|
||||
// Actual command output:
|
||||
// grep "[T\|t]x=[{]\?\"\S*\"[}]\?\|translate(\"\S*\"" -ohr './app' | grep -o "\".*\""
|
||||
const command = `grep "[T\\|t]x=[{]\\?\\"\\S*\\"[}]\\?\\|translate(\\"\\S*\\"" -ohr './app' | grep -o "\\".*\\""`
|
||||
exec(command, (_, stdout) => {
|
||||
const allTranslationsDefinedOld = iterate(en, "", [])
|
||||
// Replace first instance of "." because of i18next namespace separator
|
||||
const allTranslationsDefined = allTranslationsDefinedOld.map((key) => key.replace(".", ":"))
|
||||
const allTranslationsUsed = stdout.replace(/"/g, "").split("\n")
|
||||
allTranslationsUsed.splice(-1, 1)
|
||||
|
||||
for (let i = 0; i < allTranslationsUsed.length; i += 1) {
|
||||
if (!EXCEPTIONS.includes(allTranslationsUsed[i])) {
|
||||
// You can add keys to EXCEPTIONS (above) if you don't want them included in the test
|
||||
expect(allTranslationsDefined).toContainEqual(allTranslationsUsed[i])
|
||||
}
|
||||
}
|
||||
done()
|
||||
})
|
||||
}, 240000)
|
||||
})
|
||||
6
RN_TEMPLATE/test/mockFile.ts
Normal file
6
RN_TEMPLATE/test/mockFile.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export default {
|
||||
height: 100,
|
||||
width: 100,
|
||||
scale: 2.0,
|
||||
uri: "https://placecats.com/200/200",
|
||||
}
|
||||
58
RN_TEMPLATE/test/setup.ts
Normal file
58
RN_TEMPLATE/test/setup.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
// we always make sure 'react-native' gets included first
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import * as ReactNative from "react-native"
|
||||
|
||||
import mockFile from "./mockFile"
|
||||
|
||||
// libraries to mock
|
||||
jest.doMock("react-native", () => {
|
||||
// Extend ReactNative
|
||||
return Object.setPrototypeOf(
|
||||
{
|
||||
Image: {
|
||||
...ReactNative.Image,
|
||||
resolveAssetSource: jest.fn((_source) => mockFile), // eslint-disable-line @typescript-eslint/no-unused-vars
|
||||
getSize: jest.fn(
|
||||
(
|
||||
uri: string, // eslint-disable-line @typescript-eslint/no-unused-vars
|
||||
success: (width: number, height: number) => void,
|
||||
failure?: (_error: any) => void, // eslint-disable-line @typescript-eslint/no-unused-vars
|
||||
) => success(100, 100),
|
||||
),
|
||||
},
|
||||
},
|
||||
ReactNative,
|
||||
)
|
||||
})
|
||||
|
||||
jest.mock("i18next", () => ({
|
||||
currentLocale: "en",
|
||||
t: (key: string, params: Record<string, string>) => {
|
||||
return `${key} ${JSON.stringify(params)}`
|
||||
},
|
||||
translate: (key: string, params: Record<string, string>) => {
|
||||
return `${key} ${JSON.stringify(params)}`
|
||||
},
|
||||
}))
|
||||
|
||||
jest.mock("expo-localization", () => ({
|
||||
...jest.requireActual("expo-localization"),
|
||||
getLocales: () => [{ languageTag: "en-US", textDirection: "ltr" }],
|
||||
}))
|
||||
|
||||
jest.mock("../app/i18n/index.ts", () => ({
|
||||
i18n: {
|
||||
isInitialized: true,
|
||||
language: "en",
|
||||
t: (key: string, params: Record<string, string>) => {
|
||||
return `${key} ${JSON.stringify(params)}`
|
||||
},
|
||||
numberToCurrency: jest.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
declare const tron // eslint-disable-line @typescript-eslint/no-unused-vars
|
||||
|
||||
declare global {
|
||||
let __TEST__: boolean
|
||||
}
|
||||
8
RN_TEMPLATE/test/test-tsconfig.json
Normal file
8
RN_TEMPLATE/test/test-tsconfig.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"noImplicitAny": false,
|
||||
"noUnusedLocals": false
|
||||
},
|
||||
"include": ["**/*.test.ts", "**/*.test.tsx"]
|
||||
}
|
||||
Reference in New Issue
Block a user