Skip to content

Commit

Permalink
test: Add tests for command line usage (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattIPv4 authored Sep 19, 2022
1 parent 358cb34 commit e5a81bd
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"coverage": "nyc report --reporter=html && open coverage/index.html",
"lint": "prettier --check '{lib,test}/**/*.js' 'docs/*.md' 'tweets/README.md' README.md package.json",
"lint:fix": "prettier --write '{lib,test}/**/*.js' 'docs/*.md' 'tweets/README.md' README.md package.json",
"test": "tap --branches=50 --functions=50 --lines=50 --statements=50 test/*/test.js",
"test": "tap --branches=70 --functions=100 --lines=80 --statements=80 test/*/test.js",
"posttest": "npm run -s lint"
},
"keywords": [],
Expand Down
27 changes: 27 additions & 0 deletions test/command-line-has-invalid-tweet/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* This test checks the ability to invoke a tweet using the `--file` command line flag
* but provides a missing filename so generates an error.
*/

const nock = require("nock");
const tap = require("tap");

// MOCK
process.chdir(__dirname);
process.argv = [
"node",
"lib/index.js",
"--file",
"tweets/does-not-exist.tweet",
];

process.on("exit", (code) => {
tap.equal(code, 1);
tap.same(nock.pendingMocks(), []);

// above code exits with 1 (error), but tap expects 0.
// Tap adds the "process.exitCode" property for that purpose.
process.exitCode = 0;
});

require("../../lib");
22 changes: 22 additions & 0 deletions test/command-line-has-no-tweet/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* This test checks the ability to invoke a tweet using the `--file` command line flag
* but provides no actual filename so generates an error.
*/

const nock = require("nock");
const tap = require("tap");

// MOCK
process.chdir(__dirname);
process.argv = ["node", "lib/index.js", "--file"];

process.on("exit", (code) => {
tap.equal(code, 1);
tap.same(nock.pendingMocks(), []);

// above code exits with 1 (error), but tap expects 0.
// Tap adds the "process.exitCode" property for that purpose.
process.exitCode = 0;
});

require("../../lib");
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
media:
- file: blahaj.png
alt: Blahaj!
---

Cuddly :)
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* This test checks the ability to invoke a tweet using the `--file` command line flag
* which is in a custom directory and has front matter that includes media.
*/

const nock = require("nock");
const tap = require("tap");

// MOCK
nock("https://api.twitter.com")
.post("/1.1/statuses/update.json", (body) => {
tap.equal(body.status, "Cuddly :)\n");
tap.equal(body.media_ids, "0000000000000000002");
return true;
})
.reply(201, {
id_str: "0000000000000000001",
user: {
screen_name: "gr2m",
},
});

nock("https://upload.twitter.com")
.post("/1.1/media/upload.json", (body) => {
tap.equal(body.command, "INIT");
tap.equal(body.total_bytes, "107352");
tap.equal(body.media_type, "image/png");
return true;
})
.reply(201, {
media_id_string: "0000000000000000002",
})

.post("/1.1/media/upload.json", () => {
// TODO: Body just seems to be a string here, not an object.
// tap.equal(body.command, "APPEND");
// tap.equal(body.media_id, "0000000000000000002");
// tap.equal(body.media_type, "");
// tap.equal(body.media, "");
// tap.equal(body.segment_index, 0);
return true;
})
.reply(201)

.post("/1.1/media/upload.json", (body) => {
tap.equal(body.command, "FINALIZE");
tap.equal(body.media_id, "0000000000000000002");
return true;
})
.reply(201);

// TODO: Support alt text (twitter library does not support JSON payloads)
// https://github.com/desmondmorris/node-twitter/issues/347
// .post("/1.1/media/metadata/create.json", (body) => {
// tap.equal(body.media_id, "0000000000000000002");
// tap.equal(body.alt_text.text, "Blahaj!");
// return true;
// })
// .reply(201);

process.chdir(__dirname);
process.argv = [
"node",
"lib/index.js",
"--file",
"custom/tweets/hello-world.tweet",
];

process.on("exit", (code) => {
tap.equal(code, 0);
tap.same(nock.pendingMocks(), []);
});

require("../../lib");
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions test/command-line-has-tweet-with-front-matter-media/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* This test checks the ability to invoke a tweet using the `--file` command line flag
* which has front matter that includes media.
*/

const nock = require("nock");
const tap = require("tap");

// MOCK
nock("https://api.twitter.com")
.post("/1.1/statuses/update.json", (body) => {
tap.equal(body.status, "Cuddly :)\n");
tap.equal(body.media_ids, "0000000000000000002");
return true;
})
.reply(201, {
id_str: "0000000000000000001",
user: {
screen_name: "gr2m",
},
});

nock("https://upload.twitter.com")
.post("/1.1/media/upload.json", (body) => {
tap.equal(body.command, "INIT");
tap.equal(body.total_bytes, "107352");
tap.equal(body.media_type, "image/png");
return true;
})
.reply(201, {
media_id_string: "0000000000000000002",
})

.post("/1.1/media/upload.json", () => {
// TODO: Body just seems to be a string here, not an object.
// tap.equal(body.command, "APPEND");
// tap.equal(body.media_id, "0000000000000000002");
// tap.equal(body.media_type, "");
// tap.equal(body.media, "");
// tap.equal(body.segment_index, 0);
return true;
})
.reply(201)

.post("/1.1/media/upload.json", (body) => {
tap.equal(body.command, "FINALIZE");
tap.equal(body.media_id, "0000000000000000002");
return true;
})
.reply(201);

// TODO: Support alt text (twitter library does not support JSON payloads)
// https://github.com/desmondmorris/node-twitter/issues/347
// .post("/1.1/media/metadata/create.json", (body) => {
// tap.equal(body.media_id, "0000000000000000002");
// tap.equal(body.alt_text.text, "Blahaj!");
// return true;
// })
// .reply(201);

process.chdir(__dirname);
process.argv = ["node", "lib/index.js", "--file", "tweets/hello-world.tweet"];

process.on("exit", (code) => {
tap.equal(code, 0);
tap.same(nock.pendingMocks(), []);
});

require("../../lib");
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
media:
- file: blahaj.png
alt: Blahaj!
---

Cuddly :)
29 changes: 29 additions & 0 deletions test/command-line-has-tweet/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* This test checks the ability to invoke a tweet using the `--file` command line flag.
*/

const nock = require("nock");
const tap = require("tap");

// MOCK
nock("https://api.twitter.com")
.post("/1.1/statuses/update.json", (body) => {
tap.equal(body.status, "Hello, world!\n");
return true;
})
.reply(201, {
id_str: "0000000000000000001",
user: {
screen_name: "gr2m",
},
});

process.chdir(__dirname);
process.argv = ["node", "lib/index.js", "--file", "tweets/hello-world.tweet"];

process.on("exit", (code) => {
tap.equal(code, 0);
tap.same(nock.pendingMocks(), []);
});

require("../../lib");
1 change: 1 addition & 0 deletions test/command-line-has-tweet/tweets/hello-world.tweet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, world!

0 comments on commit e5a81bd

Please sign in to comment.