Fix the unit test toBeCalledWith deprecated warnings #182

Merged
Vylpes merged 5 commits from feature/138-unit-test-deprecated into develop 2024-06-22 10:09:56 +01:00
7 changed files with 43 additions and 25 deletions
Showing only changes of commit f315361278 - Show all commits

View file

@ -2,6 +2,8 @@ import { Command, Option } from "commander";
import randomBunny from "./index"; import randomBunny from "./index";
import ICliOptions from "./contracts/ICliOptions"; import ICliOptions from "./contracts/ICliOptions";
import { exit } from "process"; import { exit } from "process";
import OutputHelper from "./helpers/outputHelper";
import { writeFileSync } from "fs";
const program = new Command(); const program = new Command();
@ -12,6 +14,7 @@ program
.option('-s, --subreddit <subreddit>', 'The subreddit to search', 'rabbits') .option('-s, --subreddit <subreddit>', 'The subreddit to search', 'rabbits')
.option('-j, --json', 'Output as JSON') .option('-j, --json', 'Output as JSON')
.option('-q, --query-metadata', 'Include query metadata in result') .option('-q, --query-metadata', 'Include query metadata in result')
.option('-o <file>', 'Output to file')
.addOption(new Option('--sort <sort>', 'Sort by').default('hot').choices(['hot', 'new', 'top'])); .addOption(new Option('--sort <sort>', 'Sort by').default('hot').choices(['hot', 'new', 'top']));
program.parse(); program.parse();
@ -21,31 +24,14 @@ const options: ICliOptions = program.opts();
randomBunny(options.subreddit, options.sort) randomBunny(options.subreddit, options.sort)
.then((response) => { .then((response) => {
if (response.IsSuccess) { if (response.IsSuccess) {
const result = response.Result!; const output = OutputHelper.GenerateOutput(response, options);
const outputLines: string[] = []; if (options.o) {
writeFileSync(options.o, output);
if (options.json) { } else {
console.log(JSON.stringify(result)); console.log(output);
return;
} }
outputLines.push(`Archived = ${result.Archived}`);
outputLines.push(`Downvotes = ${result.Downs}`);
outputLines.push(`Hidden = ${result.Hidden}`);
outputLines.push(`Permalink = ${result.Permalink}`);
outputLines.push(`Subreddit = ${result.Subreddit}`);
outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`);
outputLines.push(`Title = ${result.Title}`);
outputLines.push(`Upvotes = ${result.Ups}`);
outputLines.push(`Url = ${result.Url}`);
if (options.queryMetadata != null) {
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
}
console.log(outputLines.join("\n"));
exit(0); exit(0);
} else { } else {
const error = response.Error!; const error = response.Error!;

View file

@ -2,5 +2,6 @@ export default interface ICliOptions {
subreddit: string, subreddit: string,
json?: boolean, json?: boolean,
sort: "new" | "hot" | "top", sort: "new" | "hot" | "top",
o?: string,
queryMetadata?: boolean, queryMetadata?: boolean,
} }

View file

@ -0,0 +1,31 @@
import ICliOptions from "../contracts/ICliOptions";
import IReturnResult from "../contracts/IReturnResult";
export default class OutputHelper {
public static GenerateOutput(response: IReturnResult, options: ICliOptions): string {
const result = response.Result!;
const outputLines: string[] = [];
if (options.json) {
return JSON.stringify(result);
}
outputLines.push(`Archived = ${result.Archived}`);
outputLines.push(`Downvotes = ${result.Downs}`);
outputLines.push(`Hidden = ${result.Hidden}`);
outputLines.push(`Permalink = ${result.Permalink}`);
outputLines.push(`Subreddit = ${result.Subreddit}`);
outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`);
outputLines.push(`Title = ${result.Title}`);
outputLines.push(`Upvotes = ${result.Ups}`);
outputLines.push(`Url = ${result.Url}`);
if (options.queryMetadata != null) {
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
}
return outputLines.join("\n");
}
}

View file

@ -5,7 +5,7 @@ import { List } from 'linqts';
import IFetchResult from "./contracts/IFetchResult"; import IFetchResult from "./contracts/IFetchResult";
import { ErrorCode } from "./constants/ErrorCode"; import { ErrorCode } from "./constants/ErrorCode";
import ErrorMessages from "./constants/ErrorMessages"; import ErrorMessages from "./constants/ErrorMessages";
import ImageHelper from "./imageHelper"; import ImageHelper from "./helpers/imageHelper";
export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot'): Promise<IReturnResult> { export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot'): Promise<IReturnResult> {
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=100`) const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=100`)

View file

@ -1,4 +1,4 @@
import ImageHelper from "../src/imageHelper"; import ImageHelper from "../../src/helpers/imageHelper";
import fetch from "got-cjs"; import fetch from "got-cjs";
jest.mock('got-cjs'); jest.mock('got-cjs');

View file

@ -1,6 +1,6 @@
import { ErrorCode } from "../src/constants/ErrorCode"; import { ErrorCode } from "../src/constants/ErrorCode";
import ErrorMessages from "../src/constants/ErrorMessages"; import ErrorMessages from "../src/constants/ErrorMessages";
import ImageHelper from "../src/imageHelper"; import ImageHelper from "../src/helpers/imageHelper";
import randomBunny from "../src/index"; import randomBunny from "../src/index";
import fetch from "got-cjs"; import fetch from "got-cjs";