Skip to content

Commit

Permalink
Add tests to all commands
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekau committed Apr 18, 2019
1 parent 251e741 commit 3a5333f
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 48 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@oclif/plugin-help": "^2",
"chalk": "^2.4.2",
"cli-ux": "^5.2.1",
"mock-require": "^3.0.3",
"puppeteer": "^1.14.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/format-output.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const chalk = require('chalk');
const isPastGame = require('./is-past-game');

module.exports = function formatOutput(data, flags) {
module.exports = function formatOutput(data, flags = {}) {
const { 'game-site': showGameSite } = flags;

if (data.isHeader) {
Expand Down
63 changes: 51 additions & 12 deletions test/commands/games/all.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
const {expect, test} = require('@oclif/test')
const { expect, test } = require('@oclif/test');
const mock = require('mock-require');

describe('games:all', () => {
before(() => {
mock('../../../src/fetchers/games', '../../mocks/fetchers/games');
mock(
'../../../src/utils/run-with-spinner',
'../../mocks/utils/run-with-spinner'
);
});

after(() => {
mock.stop('../../../src/fetchers/games');
mock.stop('../../../src/utils/run-with-spinner');
});

test
.stdout()
.command(['games:all'])
.it('runs hello', ctx => {
expect(ctx.stdout).to.contain('hello world')
})
.stdout()
.command(['games:all'])
.it('shows all games data', ctx => {
const lines = ctx.stdout.split('\n');

expect(lines.length).to.equal(6);

expect(lines[0]).to.equal('Ronda 4 - jueves 18 de abril');
expect(lines[1]).to.equal('\tTeam1 (1) - (3) Team2');
expect(lines[2]).to.equal('Ronda 5 - jueves 25 de abril');
expect(lines[3]).to.equal('\tTeam3 (2) - (4) Team4');
expect(lines[4]).to.equal('\tTeam4 () - () Team5');
expect(lines[5]).to.equal('');
});

test
.stdout()
.command(['games:all', '--name', 'jeff'])
.it('runs hello --name jeff', ctx => {
expect(ctx.stdout).to.contain('hello jeff')
})
})
.stdout()
.command(['games:all', '--game-site'])
.it(
'shows all games data including the game site when using --game-site',
ctx => {
const lines = ctx.stdout.split('\n');

expect(lines.length).to.equal(6);

expect(lines[0]).to.equal('Ronda 4 - jueves 18 de abril');
expect(lines[1]).to.equal(
'\tTeam1 (1) - (3) Team2 -> https://www.example1.com'
);
expect(lines[2]).to.equal('Ronda 5 - jueves 25 de abril');
expect(lines[3]).to.equal(
'\tTeam3 (2) - (4) Team4 -> https://www.example2.com'
);
expect(lines[4]).to.equal('\tTeam4 () - () Team5');
expect(lines[5]).to.equal('');
}
);
});
61 changes: 49 additions & 12 deletions test/commands/games/past.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,54 @@
const {expect, test} = require('@oclif/test')
const { expect, test } = require('@oclif/test');
const mock = require('mock-require');

describe('games:past', () => {
before(() => {
mock('../../../src/fetchers/games', '../../mocks/fetchers/games');
mock(
'../../../src/utils/run-with-spinner',
'../../mocks/utils/run-with-spinner'
);
});

after(() => {
mock.stop('../../../src/fetchers/games');
mock.stop('../../../src/utils/run-with-spinner');
});

test
.stdout()
.command(['games:past'])
.it('runs hello', ctx => {
expect(ctx.stdout).to.contain('hello world')
})
.stdout()
.command(['games:past'])
.it('shows all games data that have results', ctx => {
const lines = ctx.stdout.split('\n');

expect(lines.length).to.equal(5);

expect(lines[0]).to.equal('Ronda 4 - jueves 18 de abril');
expect(lines[1]).to.equal('\tTeam1 (1) - (3) Team2');
expect(lines[2]).to.equal('Ronda 5 - jueves 25 de abril');
expect(lines[3]).to.equal('\tTeam3 (2) - (4) Team4');
expect(lines[4]).to.equal('');
});

test
.stdout()
.command(['games:past', '--name', 'jeff'])
.it('runs hello --name jeff', ctx => {
expect(ctx.stdout).to.contain('hello jeff')
})
})
.stdout()
.command(['games:past', '--game-site'])
.it(
'shows all games data that have results including the game site when using --game-site',
ctx => {
const lines = ctx.stdout.split('\n');

expect(lines.length).to.equal(5);

expect(lines[0]).to.equal('Ronda 4 - jueves 18 de abril');
expect(lines[1]).to.equal(
'\tTeam1 (1) - (3) Team2 -> https://www.example1.com'
);
expect(lines[2]).to.equal('Ronda 5 - jueves 25 de abril');
expect(lines[3]).to.equal(
'\tTeam3 (2) - (4) Team4 -> https://www.example2.com'
);
expect(lines[4]).to.equal('');
}
);
});
40 changes: 27 additions & 13 deletions test/commands/games/upcoming.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
const {expect, test} = require('@oclif/test')
const { expect, test } = require('@oclif/test');
const mock = require('mock-require');

describe('games:upcoming', () => {
test
.stdout()
.command(['games:upcoming'])
.it('runs hello', ctx => {
expect(ctx.stdout).to.contain('hello world')
})
before(() => {
mock('../../../src/fetchers/games', '../../mocks/fetchers/games');
mock(
'../../../src/utils/run-with-spinner',
'../../mocks/utils/run-with-spinner'
);
});

after(() => {
mock.stop('../../../src/fetchers/games');
mock.stop('../../../src/utils/run-with-spinner');
});

test
.stdout()
.command(['games:upcoming', '--name', 'jeff'])
.it('runs hello --name jeff', ctx => {
expect(ctx.stdout).to.contain('hello jeff')
})
})
.stdout()
.command(['games:upcoming'])
.it("shows all games data that don't have results", ctx => {
const lines = ctx.stdout.split('\n');

expect(lines.length).to.equal(4);

expect(lines[0]).to.equal('Ronda 4 - jueves 18 de abril');
expect(lines[1]).to.equal('Ronda 5 - jueves 25 de abril');
expect(lines[2]).to.equal('\tTeam4 () - () Team5');
expect(lines[3]).to.equal('');
});
});
96 changes: 96 additions & 0 deletions test/mocks/fetchers/games.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
async function allGames() {
return Promise.resolve([
{
isHeader: true,
date: 'jueves 18 de abril',
round: 'Ronda 4',
},
{
isHeader: false,
teamOne: 'Team1',
teamOneResult: '1',
teamTwo: 'Team2',
teamTwoResult: '3',
gameSite: 'https://www.example1.com',
},
{
isHeader: true,
date: 'jueves 25 de abril',
round: 'Ronda 5',
},
{
isHeader: false,
teamOne: 'Team3',
teamOneResult: '2',
teamTwo: 'Team4',
teamTwoResult: '4',
gameSite: 'https://www.example2.com',
},
{
isHeader: false,
teamOne: 'Team4',
teamOneResult: '',
teamTwo: 'Team5',
teamTwoResult: '',
},
]);
}

async function pastGames() {
return Promise.resolve([
{
isHeader: true,
date: 'jueves 18 de abril',
round: 'Ronda 4',
},
{
isHeader: false,
teamOne: 'Team1',
teamOneResult: '1',
teamTwo: 'Team2',
teamTwoResult: '3',
gameSite: 'https://www.example1.com',
},
{
isHeader: true,
date: 'jueves 25 de abril',
round: 'Ronda 5',
},
{
isHeader: false,
teamOne: 'Team3',
teamOneResult: '2',
teamTwo: 'Team4',
teamTwoResult: '4',
gameSite: 'https://www.example2.com',
},
]);
}

async function upcomingGames() {
return Promise.resolve([
{
isHeader: true,
date: 'jueves 18 de abril',
round: 'Ronda 4',
},
{
isHeader: true,
date: 'jueves 25 de abril',
round: 'Ronda 5',
},
{
isHeader: false,
teamOne: 'Team4',
teamOneResult: '',
teamTwo: 'Team5',
teamTwoResult: '',
},
]);
}

module.exports = {
allGames,
pastGames,
upcomingGames,
};
3 changes: 3 additions & 0 deletions test/mocks/utils/run-with-spinner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = async function runWithSpinner(cb) {
return cb();
};
14 changes: 5 additions & 9 deletions test/utils/games-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ const gamesOptions = require('../../src/utils/games-options');

describe('gamesOptions', () => {
it('returns correct gamesOptions object', () => {
const options = {
'game-site': flags.boolean({
char: 's',
description: 'Whether or not to show a link to the game site',
default: false,
}),
};

expect(JSON.stringify(gamesOptions)).to.deep.equal(JSON.stringify(options));
expect(gamesOptions['game-site'].char).to.equal('s');
expect(gamesOptions['game-site'].description).to.equal(
'Whether or not to show a link to the game site'
);
expect(gamesOptions['game-site'].default).to.equal(false);
});
});
22 changes: 21 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ functional-red-black-tree@^1.0.1:
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
integrity sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=

get-caller-file@^1.0.1:
get-caller-file@^1.0.1, get-caller-file@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.3.tgz#f978fa4c90d1dfe7ff2d6beda2a515e713bdcf4a"
integrity sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==
Expand Down Expand Up @@ -2085,6 +2085,14 @@ mocha@^5:
mkdirp "0.5.1"
supports-color "5.4.0"

mock-require@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/mock-require/-/mock-require-3.0.3.tgz#ccd544d9eae81dd576b3f219f69ec867318a1946"
integrity sha512-lLzfLHcyc10MKQnNUCv7dMcoY/2Qxd6wJfbqCcVk3LDb8An4hF6ohk5AztrvgKhJCqj36uyzi/p5se+tvyD+Wg==
dependencies:
get-caller-file "^1.0.2"
normalize-path "^2.1.1"

mock-stdin@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/mock-stdin/-/mock-stdin-0.3.1.tgz#c657d9642d90786435c64ca5e99bbd4d09bd7dd3"
Expand Down Expand Up @@ -2152,6 +2160,13 @@ normalize-package-data@^2.3.2, normalize-package-data@^2.5.0:
semver "2 || 3 || 4 || 5"
validate-npm-package-license "^3.0.1"

normalize-path@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-2.1.1.tgz#1ab28b556e198363a8c1a6f7e6fa20137fe6aed9"
integrity sha1-GrKLVW4Zg2Oowab35vogE3/mrtk=
dependencies:
remove-trailing-separator "^1.0.1"

npm-run-path@^2.0.0:
version "2.0.2"
resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-2.0.2.tgz#35a9232dfa35d7067b4cb2ddf2357b1871536c5f"
Expand Down Expand Up @@ -2578,6 +2593,11 @@ release-zalgo@^1.0.0:
dependencies:
es6-error "^4.0.1"

remove-trailing-separator@^1.0.1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef"
integrity sha1-wkvOKig62tW8P1jg1IJJuSN52O8=

repeat-element@^1.1.2:
version "1.1.3"
resolved "https://registry.yarnpkg.com/repeat-element/-/repeat-element-1.1.3.tgz#782e0d825c0c5a3bb39731f84efee6b742e6b1ce"
Expand Down

0 comments on commit 3a5333f

Please sign in to comment.