From 5f66a10f26d64a360a5b8f4b9dbfb4650c0ae0c8 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Tue, 28 Nov 2023 18:06:47 +0000 Subject: [PATCH] Update tests --- src/cli.ts | 5 ++++- tests/cli.test.ts | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/cli.ts b/src/cli.ts index caa74b4..0de5ab3 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,6 +1,7 @@ import { Command } from "commander"; import randomBunny from "./index"; import ICliOptions from "./contracts/ICliOptions"; +import { exit } from "process"; const program = new Command(); @@ -30,10 +31,12 @@ randomBunny(options.subreddit) outputLines.push(`Upvotes = ${result.Ups}`); outputLines.push(`Url = ${result.Url}`); - console.log(outputLines.join("\n") + "\n"); + console.log(outputLines.join("\n")); + exit(0); } else { const error = response.Error!; console.error(error.Message, error.Code); + exit(1); } }); \ No newline at end of file diff --git a/tests/cli.test.ts b/tests/cli.test.ts index efa1b8d..6d105d3 100644 --- a/tests/cli.test.ts +++ b/tests/cli.test.ts @@ -1,6 +1,31 @@ import { exec } from "child_process"; import path from "path"; +describe('default', () => { + test('GIVEN no options are supplied, EXPECT standard output', async () => { + const result = await cli([], '.'); + + const keys = result.stdout.split('\n') + .flatMap(x => x.split(' = ')[0]) + .filter(x => x && x.length > 0); + const values = result.stdout.split('\n') + .flatMap(x => x.split(' = ')[1]) + .filter(x => x && x.length > 0); + + + expect(result.code).toBe(0); + expect(keys).toStrictEqual(['Archived', 'Downvotes', 'Hidden', 'Permalink', 'Subreddit', 'Subreddit Subscribers', 'Title', 'Upvotes', 'Url']); + expect(values.length).toBe(9); + }, 5000); + + test('GIVEN an error occurs, EXPECT error output', async () => { + const result = await cli(['-s', 'textonly'], '.'); + + expect(result.code).toBe(1); + expect(result.stderr).toBeDefined(); + }, 5000); +}); + describe('version', () => { test('GIVEN -V flag is supplied, EXPECT version returned', async () => { const result = await cli(['-V'], '.'); @@ -31,6 +56,38 @@ describe('help', () => { expect(result.code).toBe(0); expect(result.stdout.split('\n')[0]).toBe('Usage: random-bunny [options]'); }); +}); + +describe('subreddit', () => { + test('GIVEN -s is not supplied, EXPECT subreddit to be defaulted', async () => { + const result = await cli([], '.'); + + const subreddit = result.stdout.split('\n') + .find(x => x && x.length > 0 && x.split(' = ')[0] == 'Subreddit')! + .split(' = ')[1]; + + expect(subreddit).toBe('Rabbits'); + }, 5000); + + test('GIVEN -s is supplied, EXPECT subreddit to be changed', async () => { + const result = await cli(['-s', 'horses'], '.'); + + const subreddit = result.stdout.split('\n') + .find(x => x && x.length > 0 && x.split(' = ')[0] == 'Subreddit')! + .split(' = ')[1]; + + expect(subreddit).toBe('Horses'); + }, 5000); + + test('GIVEN --subreddit is supplied, EXPECT subreddit to be changed', async () => { + const result = await cli(['--subreddit', 'horses'], '.'); + + const subreddit = result.stdout.split('\n') + .find(x => x && x.length > 0 && x.split(' = ')[0] == 'Subreddit')! + .split(' = ')[1]; + + expect(subreddit).toBe('Horses'); + }, 5000); }) function cli(args: string[], cwd: string): Promise {