@crowdin/app-project-module
Version:
Module that generates for you all common endpoints for serving standalone Crowdin App
143 lines (142 loc) • 7.58 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateCrowdinTest = void 0;
const globals_1 = require("@jest/globals");
const crowdin_api_client_1 = __importDefault(require("@crowdin/crowdin-api-client"));
const types_1 = require("../../modules/integration/util/types");
const index_1 = require("./index");
const crowdin_storage_1 = require("./mocks/crowdin-storage");
const util_1 = require("../util");
const getParents = (node, expectedTree, parentNames = []) => {
if (node.parentId) {
const parent = expectedTree.find(({ id }) => id === node.parentId);
if (parent) {
parentNames.unshift(parent.name);
return getParents(parent, expectedTree, parentNames);
}
}
return parentNames;
};
const getExpectedFolders = ({ integrationTestConfig, expectedTree, updateRequest, }) => {
var _a, _b;
if ((_b = (_a = integrationTestConfig.updateCrowdin) === null || _a === void 0 ? void 0 : _a.expected) === null || _b === void 0 ? void 0 : _b.folders) {
return integrationTestConfig.updateCrowdin.expected.folders;
}
const expectedDirectoriesByPath = {};
updateRequest.forEach((node) => {
const parentNames = getParents(node, expectedTree);
if (parentNames.length) {
const fullPath = `/${parentNames.join('/')}`;
expectedDirectoriesByPath[fullPath] = { path: fullPath, name: parentNames.pop() || '' };
}
});
return Object.values(expectedDirectoriesByPath);
};
const getExpectedFiles = ({ integrationTestConfig, expectedTree, updateRequest, }) => {
var _a, _b;
if ((_b = (_a = integrationTestConfig.updateCrowdin) === null || _a === void 0 ? void 0 : _a.expected) === null || _b === void 0 ? void 0 : _b.files) {
return integrationTestConfig.updateCrowdin.expected.files;
}
const expectedFilesByPath = {};
updateRequest.forEach((node) => {
const parentNames = getParents(node, expectedTree);
parentNames.push(`${node.name}.${node.type}`);
const fullPath = `/${parentNames.join('/')}`;
expectedFilesByPath[fullPath] = { path: fullPath, name: parentNames.pop() || '' };
});
return Object.values(expectedFilesByPath);
};
const getFilesFromTree = (tree) => {
return tree
.filter((item) => 'type' in item)
.filter(({ type }) => type)
.map((file) => file);
};
const updateCrowdinTest = ({ appConfig, integrationTestConfig, }) => __awaiter(void 0, void 0, void 0, function* () {
const { updateCrowdin, appSettings, integrationCredentials: credentials } = integrationTestConfig;
const expectedTree = integrationTestConfig.getIntegrationFiles.expected;
if (!appConfig.projectIntegration) {
throw new Error('The app configuration is missing the projectIntegration module.');
}
const client = new crowdin_api_client_1.default({ token: 'fakeToken' });
const request = getFilesFromTree(expectedTree);
const updateProgressMock = jest.fn(() => __awaiter(void 0, void 0, void 0, function* () {
return { isCanceled: false };
}));
const rootFolder = (0, crowdin_storage_1.getRootFolder)(appConfig);
if (rootFolder) {
index_1.mockDirectories.push(rootFolder);
}
const result = yield (0, util_1.assert)(() => {
var _a;
return (_a = appConfig.projectIntegration) === null || _a === void 0 ? void 0 : _a.updateCrowdin({
projectId: index_1.mockProjectId,
client,
credentials,
request,
rootFolder,
appSettings,
uploadTranslations: false,
job: {
get: function getJob() {
return __awaiter(this, void 0, void 0, function* () {
return {
id: 'jobId1',
integrationId: 'string',
crowdinId: 'string',
type: types_1.JobType.UPDATE_TO_CROWDIN,
title: 'Sync files to Crowdin',
progress: 0,
status: types_1.JobStatus.CREATED,
createdAt: 0,
};
});
},
update: updateProgressMock,
type: types_1.JobClientType.MANUAL,
fetchTranslation: jest.fn(),
translationUploaded: jest.fn(),
markFilesAsUnsynced: jest.fn(),
unmarkFilesAsUnsynced: jest.fn(),
},
});
}, 'Fail to run method updateCrowdin()');
if (!(updateCrowdin === null || updateCrowdin === void 0 ? void 0 : updateCrowdin.updateProgressDisabled)) {
yield (0, util_1.assert)(() => (0, globals_1.expect)(updateProgressMock.mock.calls.length).toBeGreaterThanOrEqual(request.length), 'The sync progress should be updated at least once for each file in request');
}
const expectedFiles = getExpectedFiles({
integrationTestConfig,
expectedTree,
updateRequest: request,
});
yield (0, util_1.assert)(() => (0, globals_1.expect)(index_1.mockFiles).toHaveLength(expectedFiles.length), `Files created: ${index_1.mockFiles.length}, but expected: ${expectedFiles.length}`);
yield (0, util_1.assert)(() => (0, globals_1.expect)(index_1.mockFiles).toEqual(globals_1.expect.arrayContaining(expectedFiles.map((f) => globals_1.expect.objectContaining(f)))), 'The properties of the created files do not match to the expected');
const expectedDirectories = getExpectedFolders({
integrationTestConfig,
expectedTree,
updateRequest: request,
});
const createdDirectories = rootFolder
? index_1.mockDirectories.filter(({ id }) => rootFolder && id !== rootFolder.id)
: index_1.mockDirectories;
yield (0, util_1.assert)(() => (0, globals_1.expect)(createdDirectories).toHaveLength(expectedDirectories.length), `Directories created: ${createdDirectories.length}, but expected: ${expectedDirectories.length}`);
yield (0, util_1.assert)(() => (0, globals_1.expect)(createdDirectories).toEqual(globals_1.expect.arrayContaining(expectedDirectories.map((f) => globals_1.expect.objectContaining(f)))), 'The properties of the created directories do not match to the expected');
if (updateCrowdin === null || updateCrowdin === void 0 ? void 0 : updateCrowdin.extraChecks) {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
yield (0, util_1.assert)(() => updateCrowdin.extraChecks({ request, result }), 'ExtraChecks for updateCrowdin() fails');
}
});
exports.updateCrowdinTest = updateCrowdinTest;