Update tests
This commit is contained in:
parent
f315361278
commit
d21a6b4dba
6 changed files with 273 additions and 207 deletions
29
tests/helpers/__snapshots__/outputHelper.test.ts.snap
Normal file
29
tests/helpers/__snapshots__/outputHelper.test.ts.snap
Normal file
|
@ -0,0 +1,29 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`GenerateOutput EXPECT standout output to be returned 1`] = `
|
||||
"Archived = false
|
||||
Downvotes = 0
|
||||
Hidden = false
|
||||
Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/
|
||||
Subreddit = Rabbits
|
||||
Subreddit Subscribers = 654751
|
||||
Title = This is my Ms Bear!
|
||||
Upvotes = 17
|
||||
Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"
|
||||
`;
|
||||
|
||||
exports[`GenerateOutput GIVEN options.json is true, EXPECT output to be returned as JSON 1`] = `"{"Archived":false,"Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"}"`;
|
||||
|
||||
exports[`GenerateOutput GIVEN options.queryMetadata is supplied, EXPECT query metadata to be added 1`] = `
|
||||
"Archived = false
|
||||
Downvotes = 0
|
||||
Hidden = false
|
||||
Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/
|
||||
Subreddit = Rabbits
|
||||
Subreddit Subscribers = 654751
|
||||
Title = This is my Ms Bear!
|
||||
Upvotes = 17
|
||||
Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d
|
||||
Query.Subreddit = rabbits
|
||||
Query.Sort By = hot"
|
||||
`;
|
118
tests/helpers/cliHelper.test.ts
Normal file
118
tests/helpers/cliHelper.test.ts
Normal file
|
@ -0,0 +1,118 @@
|
|||
import fs from "fs";
|
||||
import CliHelper from "../../src/helpers/cliHelper";
|
||||
import ICliOptions from "../../src/contracts/ICliOptions";
|
||||
import IReturnResult from "../../src/contracts/IReturnResult";
|
||||
import OutputHelper from "../../src/helpers/outputHelper";
|
||||
import { ErrorCode } from "../../src/constants/ErrorCode";
|
||||
|
||||
describe("Endpoint", () => {
|
||||
describe("GIVEN response is successful", () => {
|
||||
test("GIVEN options.o is defined, EXPECT output written to file", () => {
|
||||
// Arrange
|
||||
const response = {
|
||||
IsSuccess: true,
|
||||
} as IReturnResult;
|
||||
|
||||
const options = {
|
||||
o: "file.txt",
|
||||
} as ICliOptions;
|
||||
|
||||
OutputHelper.GenerateOutput = jest.fn().mockReturnValue("test output");
|
||||
|
||||
fs.writeFileSync = jest.fn();
|
||||
|
||||
console.log = jest.fn();
|
||||
|
||||
console.error = jest.fn();
|
||||
|
||||
// Act
|
||||
const result = CliHelper.Endpoint(response, options);
|
||||
|
||||
// Assert
|
||||
expect(result).toBe(0);
|
||||
|
||||
expect(OutputHelper.GenerateOutput).toHaveBeenCalledTimes(1);
|
||||
expect(OutputHelper.GenerateOutput).toHaveBeenCalledWith(response, options);
|
||||
|
||||
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
|
||||
expect(fs.writeFileSync).toHaveBeenCalledWith("file.txt", "test output");
|
||||
|
||||
expect(console.log).not.toHaveBeenCalled();
|
||||
|
||||
expect(console.error).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("GIVEN options.o is undefined, EXPECT output logged to console", () => {
|
||||
// Arrange
|
||||
const response = {
|
||||
IsSuccess: true,
|
||||
} as IReturnResult;
|
||||
|
||||
const options = {
|
||||
o: undefined,
|
||||
} as ICliOptions;
|
||||
|
||||
OutputHelper.GenerateOutput = jest.fn().mockReturnValue("test output");
|
||||
|
||||
fs.writeFileSync = jest.fn();
|
||||
|
||||
console.log = jest.fn();
|
||||
|
||||
console.error = jest.fn();
|
||||
|
||||
// Act
|
||||
const result = CliHelper.Endpoint(response, options);
|
||||
|
||||
// Assert
|
||||
expect(result).toBe(0);
|
||||
|
||||
expect(OutputHelper.GenerateOutput).toHaveBeenCalledTimes(1);
|
||||
expect(OutputHelper.GenerateOutput).toHaveBeenCalledWith(response, options);
|
||||
|
||||
expect(fs.writeFileSync).not.toHaveBeenCalled();
|
||||
|
||||
expect(console.log).toHaveBeenCalledTimes(1);
|
||||
expect(console.log).toHaveBeenCalledWith("test output");
|
||||
|
||||
expect(console.error).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
test("GIVEN response is failure, EXPECT error logged to console", () => {
|
||||
// Arrange
|
||||
const response = {
|
||||
IsSuccess: false,
|
||||
Error: {
|
||||
Message: "error message",
|
||||
Code: ErrorCode.FailedToFetchReddit,
|
||||
},
|
||||
} as IReturnResult;
|
||||
|
||||
const options = {
|
||||
o: "file.txt",
|
||||
} as ICliOptions;
|
||||
|
||||
OutputHelper.GenerateOutput = jest.fn().mockReturnValue("test output");
|
||||
|
||||
fs.writeFileSync = jest.fn();
|
||||
|
||||
console.log = jest.fn();
|
||||
|
||||
console.error = jest.fn();
|
||||
|
||||
// Act
|
||||
const result = CliHelper.Endpoint(response, options);
|
||||
|
||||
// Assert
|
||||
expect(result).toBe(1);
|
||||
|
||||
expect(OutputHelper.GenerateOutput).not.toHaveBeenCalled();
|
||||
|
||||
expect(fs.writeFileSync).not.toHaveBeenCalled();
|
||||
|
||||
expect(console.log).not.toHaveBeenCalled();
|
||||
|
||||
expect(console.error).toHaveBeenCalledTimes(1);
|
||||
expect(console.error).toHaveBeenCalledWith("error message", ErrorCode.FailedToFetchReddit);
|
||||
});
|
||||
});
|
99
tests/helpers/outputHelper.test.ts
Normal file
99
tests/helpers/outputHelper.test.ts
Normal file
|
@ -0,0 +1,99 @@
|
|||
import ICliOptions from "../../src/contracts/ICliOptions";
|
||||
import IReturnResult from "../../src/contracts/IReturnResult";
|
||||
import OutputHelper from "../../src/helpers/outputHelper";
|
||||
|
||||
describe("GenerateOutput", () => {
|
||||
test("EXPECT standout output to be returned", () => {
|
||||
// Arrange
|
||||
const response = {
|
||||
IsSuccess: true,
|
||||
Query: {
|
||||
subreddit: "rabbits",
|
||||
sortBy: "hot",
|
||||
},
|
||||
Result: {
|
||||
Archived: false,
|
||||
Downs: 0,
|
||||
Hidden: false,
|
||||
Permalink: "/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/",
|
||||
Subreddit: "Rabbits",
|
||||
SubredditSubscribers: 654751,
|
||||
Title: "This is my Ms Bear!",
|
||||
Ups: 17,
|
||||
Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d",
|
||||
},
|
||||
} as IReturnResult;
|
||||
|
||||
const options = {} as ICliOptions;
|
||||
|
||||
// Act
|
||||
const result = OutputHelper.GenerateOutput(response, options);
|
||||
|
||||
// Assert
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test("GIVEN options.json is true, EXPECT output to be returned as JSON", () => {
|
||||
// Arrange
|
||||
const response = {
|
||||
IsSuccess: true,
|
||||
Query: {
|
||||
subreddit: "rabbits",
|
||||
sortBy: "hot",
|
||||
},
|
||||
Result: {
|
||||
Archived: false,
|
||||
Downs: 0,
|
||||
Hidden: false,
|
||||
Permalink: "/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/",
|
||||
Subreddit: "Rabbits",
|
||||
SubredditSubscribers: 654751,
|
||||
Title: "This is my Ms Bear!",
|
||||
Ups: 17,
|
||||
Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d",
|
||||
},
|
||||
} as IReturnResult;
|
||||
|
||||
const options = {
|
||||
json: true,
|
||||
} as ICliOptions;
|
||||
|
||||
// Act
|
||||
const result = OutputHelper.GenerateOutput(response, options);
|
||||
|
||||
// Assert
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
|
||||
test("GIVEN options.queryMetadata is supplied, EXPECT query metadata to be added", () => {
|
||||
// Arrange
|
||||
const response = {
|
||||
IsSuccess: true,
|
||||
Query: {
|
||||
subreddit: "rabbits",
|
||||
sortBy: "hot",
|
||||
},
|
||||
Result: {
|
||||
Archived: false,
|
||||
Downs: 0,
|
||||
Hidden: false,
|
||||
Permalink: "/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/",
|
||||
Subreddit: "Rabbits",
|
||||
SubredditSubscribers: 654751,
|
||||
Title: "This is my Ms Bear!",
|
||||
Ups: 17,
|
||||
Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d",
|
||||
},
|
||||
} as IReturnResult;
|
||||
|
||||
const options = {
|
||||
queryMetadata: true,
|
||||
} as ICliOptions;
|
||||
|
||||
// Act
|
||||
const result = OutputHelper.GenerateOutput(response, options);
|
||||
|
||||
// Assert
|
||||
expect(result).toMatchSnapshot();
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue