Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TA-2683] Setup tests and add tests for listing assignments #161

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
- name: Yarn Build
run: yarn build

- name: Yarn Test
run: yarn test

- name: Move files to build
run: |
cp README.md dist/README.md
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ jobs:
run: yarn install
- name: Yarn Build
run: yarn build
- name: Yarn Test
run: yarn test
13 changes: 13 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Config } from "@jest/types"
const config: Config.InitialOptions = {
verbose: true,
transform: {
"^.+\\.tsx?$": "ts-jest",
},
testMatch: ["<rootDir>/tests/**/*.spec.ts"],
moduleNameMapper: {
"^\\./../package.json$": "<rootDir>/tests/mocks/package.json",
}
}

export default config
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"homepage": "https://github.com/celonis/content-cli",
"scripts": {
"build": "./node_modules/.bin/tsc && cp package.json dist/package.json",
"lint": "tslint -p ."
"lint": "tslint -p .",
"test": "jest"
},
"dependencies": {
"@datadog/datadog-ci": "^0.17.12",
Expand All @@ -26,9 +27,13 @@
},
"devDependencies": {
"@types/adm-zip": "^0.4.34",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.4",
"jest": "^29.7.0",
"lint-staged": "^9.5.0",
"prettier": "^1.19.1",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.2",
"tslint": "^5.20.1",
"tslint-config-prettier": "^1.18.0",
"tslint-consistent-codestyle": "^1.16.0",
Expand Down
81 changes: 81 additions & 0 deletions tests/list/assignments.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {VariableCommand} from "../../src/commands/variable.command";
import axios from "axios";
import {logger} from "../../src/util/logger";
import {TestTransport} from "../utls/test-transport";
import {setDefaultProfile} from "../utls/context-mock";
import {FileService} from "../../src/services/file-service";
import * as fs from "fs";
import * as path from "path";

jest.mock("axios");
jest.mock("fs");

describe("List assignments", () => {
let testTransport;

beforeEach(() => {
testTransport = new TestTransport({})
logger.add(testTransport);

setDefaultProfile();
})

it("Should list assignments for supported type and non-json response", async () => {
const mockAssignmentValues = [
{id: "id-1"},
{id: "id-2"}
];
const resp = {data: mockAssignmentValues};
(axios.get as jest.Mock).mockResolvedValue(resp);

await new VariableCommand().listAssignments("DATA_MODEL", false, "");

expect(testTransport.logMessages.length).toBe(2);
expect(testTransport.logMessages[0].message).toContain('{"id":"id-1"}');
expect(testTransport.logMessages[1].message).toContain('{"id":"id-2"}');

expect(axios.get).toHaveBeenCalledWith("https://myTeam.celonis.cloud/package-manager/api/compute-pools/pools-with-data-models", expect.anything())
})

it("Should export assignments for supported type and json response", async () => {
const mockAssignmentValues = [
{id: "id-1"},
{id: "id-2"}
];
const resp = {data: mockAssignmentValues};
(axios.get as jest.Mock).mockResolvedValue(resp);

(fs.writeFileSync as jest.Mock).mockImplementation();

await new VariableCommand().listAssignments("DATA_MODEL", true, "");

expect(testTransport.logMessages.length).toBe(1);
expect(testTransport.logMessages[0].message).toContain(FileService.fileDownloadedMessage);

const expectedFileName = testTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];

expect(fs.writeFileSync).toHaveBeenCalledWith(path.resolve(process.cwd(), expectedFileName), JSON.stringify(mockAssignmentValues), {encoding: "utf-8"});
})

it("Should contain url params in the url", async () => {
const mockAssignmentValues = [{id: "id-1"}];
const resp = {data: mockAssignmentValues};
(axios.get as jest.Mock).mockResolvedValue(resp);

await new VariableCommand().listAssignments("CONNECTION", false, "param1=value1,param2=value2");

expect(axios.get).toHaveBeenCalledWith("https://myTeam.celonis.cloud/process-automation-v2/api/connections?param1=value1&param2=value2", expect.anything())
})

it("Should throw error for unsupported variable types", async () => {
const type: string = "DUMMY_UNSUPPORTED_TYPE";

try {
await new VariableCommand().listAssignments(type, false, "");
} catch (e) {
if (!(e.message === `Variable type ${type} not supported.`)) {
fail();
}
}
})
})
3 changes: 3 additions & 0 deletions tests/mocks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "mocked-version"
}
12 changes: 12 additions & 0 deletions tests/utls/context-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {contextService} from "../../src/services/context.service";

export function setDefaultProfile(): void {
contextService.setContext({
profile: {
name: "test",
team: "https://myTeam.celonis.cloud/",
apiToken: "YnQ3N2M0M2ItYzQ3OS00YzgyLTg0ODgtOWNkNzhiNzYwOTU2OlFkNnBpVCs0M0JBYm1ZWGlCZ2hPd245aldwWTNubFQyYVFOTFBUeHEwdUxM",
authenticationType: "Bearer"
}
});
}
15 changes: 15 additions & 0 deletions tests/utls/test-transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as Transport from "winston-transport";
import {LogEntry} from "winston";

export class TestTransport extends Transport {
public logMessages: LogEntry[] = [];

constructor(options: any) {
super(options);
}

public log(info: LogEntry, callback: () => void): void {
this.logMessages.push(info);
callback();
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
},
"exclude": ["./tests", "./jest.config.ts"]
}
Loading