Skip to content

Commit 2b87247

Browse files
committed
Added tests
+ Mocha testing
1 parent c8e6d44 commit 2b87247

File tree

2 files changed

+144
-2
lines changed

2 files changed

+144
-2
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "cataas-api",
3-
"version": "1.0.1",
3+
"version": "1.0.41",
44
"description": "Cat as a service API wrapper for javascirpt",
55
"main": "index.js",
66
"types": "index.d.ts",
77
"scripts": {
8-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "./node_modules/.bin/mocha tests/index.js"
99
},
1010
"repository": {
1111
"type": "git",
@@ -29,5 +29,8 @@
2929
"dependencies": {
3030
"fs": "0.0.1-security",
3131
"http": "0.0.1-security"
32+
},
33+
"devDependencies": {
34+
"mocha": "^8.3.2"
3235
}
3336
}

tests/index.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
const Cataas = require('..');
2+
const assert = require('assert');
3+
const { describe } = require('mocha');
4+
const { unlinkSync } = require('fs');
5+
6+
describe('Cataas', () => {
7+
describe('constructor()', () => {
8+
describe('options.Gif == false by default', () => {
9+
it('with options = undefined', () => {
10+
assert(!new Cataas().options.Gif, 'gif == false')
11+
})
12+
it('with option = {}', () => {
13+
assert(!new Cataas({}).options.Gif, 'gif == false')
14+
})
15+
})
16+
})
17+
18+
describe('encode()', () => {
19+
describe('path', () => {
20+
it('with gif', () => {
21+
const cataas = new Cataas({
22+
Gif: true,
23+
Text: 'meow',
24+
})
25+
const expected = "https://cataas.com/cat/gif/says/meow"
26+
const actual = cataas
27+
.encode()
28+
.toString()
29+
.toLowerCase()
30+
31+
assert(actual.startsWith(expected), 'starts with path')
32+
})
33+
34+
it('with tag', () => {
35+
const cataas = new Cataas({
36+
Tag: 'cute',
37+
Text: 'meow',
38+
})
39+
const expected = "https://cataas.com/cat/cute/says/meow"
40+
const actual = cataas
41+
.encode()
42+
.toString()
43+
.toLowerCase()
44+
45+
assert(actual.startsWith(expected), 'starts with path')
46+
})
47+
48+
it('no tag', () => {
49+
const cataas = new Cataas({
50+
Text: 'meow',
51+
})
52+
const expected = "https://cataas.com/cat/says/meow"
53+
const actual = cataas
54+
.encode()
55+
.toString()
56+
.toLowerCase()
57+
58+
assert(actual.startsWith(expected), 'starts with path')
59+
})
60+
})
61+
62+
it('queries', () => {
63+
const cataas = new Cataas({
64+
Filter: 'mono',
65+
Width: 100,
66+
Height: 200,
67+
Size: 'md',
68+
Text: '.',
69+
TextSize: 34,
70+
TextColor: 'blue',
71+
})
72+
const expected = [
73+
'size=34',
74+
'type=md',
75+
'width=100',
76+
'color=blue',
77+
'height=200',
78+
'filter=mono',
79+
]
80+
const actual = cataas
81+
.encode()
82+
.searchParams
83+
.toString()
84+
.replace('?', '')
85+
.toLowerCase()
86+
.split('&')
87+
88+
assert(expected.every(v => actual.includes(v)))
89+
})
90+
})
91+
92+
it('encodeById(id)', () => {
93+
const c = new Cataas()
94+
const r = Math.ceil(Math.random() * 1e8).toString()
95+
const actual = c.encodeById(r)
96+
assert(actual.pathname.endsWith(r))
97+
})
98+
99+
it('download(path)', async () => {
100+
const c = new Cataas()
101+
const path = 'cat.png'
102+
c.encode()
103+
await c.download(path)
104+
.then(assert)
105+
.catch(assert.ok)
106+
107+
unlinkSync(path)
108+
})
109+
110+
it('getAllTags()', async () => {
111+
const c = new Cataas()
112+
c.encode()
113+
await c.getAllTags()
114+
.then(tags => assert(tags.length > 0))
115+
.catch(assert.ok)
116+
})
117+
118+
describe('getCats(tags, options)', () => {
119+
it('with option', async () => {
120+
var cataas = new Cataas()
121+
cataas.encode()
122+
await cataas.getCats(['cute'], { Limit: 5 })
123+
.then(cats => assert(cats.length == 5))
124+
.catch(assert.ok)
125+
})
126+
127+
it('without option', async () => {
128+
var cataas = new Cataas()
129+
cataas.encode()
130+
await cataas.getCats(['cute'])
131+
.then(cats => {
132+
assert(cats.length > 0)
133+
assert(cats.every(cat => cat.tags.length > 0))
134+
assert(cats.every(cat => cat.id ? true : false))
135+
})
136+
.catch(assert.ok)
137+
})
138+
})
139+
})

0 commit comments

Comments
 (0)