Skip to content

Commit bfc0077

Browse files
authored
test(firestore-translate-text): Implement tests for firestore-translate-text (#67)
1 parent 2b8bbc2 commit bfc0077

25 files changed

+1313
-556
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ README.md
99
**/functions/lib/**
1010
**/lib/**
1111
**/dist/**
12+
coverage
1213

1314
# extension install md files
1415
# - excluded as prettier escapes variables e.g. `${PROJECT_ID}` becomes `\${PROJECT_ID}`

.travis.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
language: node_js
22

3+
install:
4+
- npm install -g codecov
5+
- npm install
6+
37
node_js:
48
- 8
59
- 10
@@ -11,11 +15,10 @@ stages:
1115

1216
jobs:
1317
include:
14-
- stage: validate
15-
name: "TypeScript Compile"
16-
script: npm run clean && npm run build
1718
- stage: validate
1819
name: "Prettier Format Check"
1920
script: npm run lint
20-
- stage: test
21-
script: npm run test-coverage
21+
22+
script:
23+
- npm run test-coverage
24+
- codecov

codecov.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
codecov:
2+
require_ci_to_pass: yes
3+
4+
coverage:
5+
precision: 2
6+
round: down
7+
range: "40...100"
8+
9+
status:
10+
project:
11+
default: true
12+
13+
comment:
14+
layout: "reach, diff, files"
15+
behavior: once
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`extension config config loaded from environment variables 1`] = `
4+
Object {
5+
"inputFieldName": "input",
6+
"languages": Array [
7+
"en",
8+
"es",
9+
"de",
10+
"fr",
11+
],
12+
"location": "us-central1",
13+
"outputFieldName": "translated",
14+
}
15+
`;
16+
17+
exports[`extension config config.LANGUAGES param exists 1`] = `
18+
Object {
19+
"default": "en,es,de,fr",
20+
"description": "Into which target languages do you want to translate new strings? The languages are identified using ISO-639-1 codes in a comma-separated list, for example: en,es,de,fr. For these codes, visit the [supported languages list](https://cloud.google.com/translate/docs/languages).
21+
",
22+
"example": "en,es,de,fr",
23+
"label": "Target languages for translations, as a comma-separated list",
24+
"param": "LANGUAGES",
25+
"required": true,
26+
"validationErrorMessage": "Languages must be a comma-separated list of ISO-639-1 language codes.",
27+
"validationRegex": "^[a-zA-Z,-]*[a-zA-Z-]{2,}$",
28+
}
29+
`;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { readFileSync } from "fs";
2+
import { resolve as pathResolve } from "path";
3+
import * as functionsTestInit from "firebase-functions-test";
4+
5+
import * as yaml from "js-yaml";
6+
import mockedEnv from "mocked-env";
7+
8+
import { messages } from "../functions/src/logs/messages";
9+
10+
let restoreEnv;
11+
let extensionYaml;
12+
let extensionParams;
13+
14+
const environment = {
15+
LOCATION: "us-central1",
16+
LANGUAGES: "en,es,de,fr,en", // double en to test no duplicates
17+
COLLECTION_PATH: "translations",
18+
INPUT_FIELD_NAME: "input",
19+
OUTPUT_FIELD_NAME: "translated",
20+
};
21+
22+
const { mockConsoleLog, config } = global;
23+
24+
functionsTestInit();
25+
26+
describe("extension config", () => {
27+
beforeAll(() => {
28+
extensionYaml = yaml.safeLoad(
29+
readFileSync(pathResolve(__dirname, "../extension.yaml"), "utf8")
30+
);
31+
32+
extensionParams = extensionYaml.params.reduce((obj, param) => {
33+
obj[param.param] = param;
34+
return obj;
35+
}, {});
36+
});
37+
38+
beforeEach(() => {
39+
restoreEnv = mockedEnv(environment);
40+
mockConsoleLog.mockClear();
41+
});
42+
43+
afterEach(() => restoreEnv());
44+
45+
test("config loaded from environment variables", () => {
46+
const functionsConfig = config();
47+
48+
expect(functionsConfig).toMatchSnapshot({});
49+
});
50+
51+
test("config is logged on initialize", () => {
52+
jest.requireActual("../functions/src");
53+
54+
const functionsConfig = config();
55+
56+
expect(mockConsoleLog).toBeCalledWith(...messages.init(functionsConfig));
57+
});
58+
59+
// LANGUAGES
60+
describe("config.LANGUAGES", () => {
61+
test("param exists", () => {
62+
const extensionParam = extensionParams["LANGUAGES"];
63+
expect(extensionParam).toMatchSnapshot();
64+
});
65+
66+
test("removes any duplicated languages from user input", () => {
67+
const functionsConfig = require("../functions/src/config").default;
68+
expect(functionsConfig.languages).toEqual(["en", "es", "de", "fr"]);
69+
});
70+
71+
describe("validationRegex", () => {
72+
test("does not allow empty strings", () => {
73+
const { validationRegex } = extensionParams["LANGUAGES"];
74+
expect(Boolean("".match(new RegExp(validationRegex)))).toBeFalsy();
75+
});
76+
77+
test("does not allow trailing delimiter", () => {
78+
const { validationRegex } = extensionParams["LANGUAGES"];
79+
expect(Boolean("en,".match(new RegExp(validationRegex)))).toBeFalsy();
80+
});
81+
82+
// https://github.com/firebase/extensions/issues/43
83+
test("allows a single language", () => {
84+
const { validationRegex } = extensionParams["LANGUAGES"];
85+
expect(Boolean("en".match(new RegExp(validationRegex)))).toBeTruthy();
86+
});
87+
88+
test("allows multiple languages", () => {
89+
const { validationRegex } = extensionParams["LANGUAGES"];
90+
91+
expect(
92+
Boolean("en,fr".match(new RegExp(validationRegex)))
93+
).toBeTruthy();
94+
95+
expect(
96+
Boolean("en,fr,de,es".match(new RegExp(validationRegex)))
97+
).toBeTruthy();
98+
});
99+
});
100+
});
101+
});

0 commit comments

Comments
 (0)