Skip to content

Commit 9ef866d

Browse files
committed
Add unit tests
1 parent 7f0569d commit 9ef866d

File tree

6 files changed

+100
-17
lines changed

6 files changed

+100
-17
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
> Request an http(s) url and *scrape its metadata*. It requires [node-html-parser](https://github.com/taoqf/node-html-parser)
44
5+
<p align="left">
6+
<a href="https://travis-ci.org/webhacking/url-metadata-parser"><img src="https://travis-ci.org/webhacking/url-metadata-parser.svg?branch=master" alt="Build Status"></a>
7+
<a href="https://codecov.io/gh/webhacking/sequence-shorten"><img src="https://codecov.io/gh/webhacking/sequence-shorten/branch/master/graph/badge.svg" /></a>
8+
</p>
9+
10+
511
## Install
612

713
```

package.json

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "dist/index.js",
66
"scripts": {
77
"format": "./node_modules/.bin/prettier --write \"**/*.ts\"",
8-
"test": "./node_modules/.bin/jest --testPathIgnorePatterns dist",
8+
"test": "./node_modules/.bin/jest",
99
"coverage": "./node_modules/.bin/jest --coverage --testPathIgnorePatterns dist",
1010
"post-coverage": "./node_modules/.bin/nyc report --reporter=json > coverage/coverage.json"
1111
},
@@ -26,13 +26,36 @@
2626
"iconv-lite": "^0.4.24",
2727
"node-html-parser": "^1.1.12",
2828
"rxjs": "^6.3.3",
29-
"ts-node": "^8.0.2",
30-
"typescript": "^3.2.4"
29+
"ts-node": "^8.0.2"
3130
},
3231
"devDependencies": {
3332
"@types/axios": "^0.14.0",
3433
"@types/iconv": "^2.1.16",
3534
"@types/iconv-lite": "0.0.1",
36-
"prettier": "^1.17.0"
35+
"@types/jest": "^24.0.11",
36+
"jest": "^24.7.1",
37+
"prettier": "^1.17.0",
38+
"ts-jest": "^24.0.2",
39+
"typescript": "^3.4.4"
40+
},
41+
"jest": {
42+
"transform": {
43+
"^.+\\.ts$": "ts-jest"
44+
},
45+
"testRegex": "\\.spec\\.ts$",
46+
"collectCoverage": true,
47+
"moduleFileExtensions": [
48+
"ts",
49+
"js"
50+
],
51+
"globals": {
52+
"ts-jest": {
53+
"enableTsDiagnostics": true
54+
}
55+
},
56+
"testPathIgnorePatterns": [
57+
"./dist",
58+
"./node_modules"
59+
]
3760
}
3861
}

src/meta.entity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export class MetaEntity {
1212
}
1313

1414
public getContentByName(name: string): string | null {
15-
return this.tags.map((tag) => (tag.node.childNodes[0] as HTMLElement).attributes).filter(tag => tag.name && tag.name == name)[0] ? this.tags.map((tag) => (tag.node.childNodes[0] as HTMLElement).attributes).filter(tag => tag.name && tag.name === name)[0].content : null;
15+
return this.tags.map((tag) => (tag.node.childNodes[0] as HTMLElement).attributes).filter(tag => tag.name && tag.name === name)[0] ? this.tags.map((tag) => (tag.node.childNodes[0] as HTMLElement).attributes).filter(tag => tag.name && tag.name === name)[0].content : null;
1616
}
1717

1818
public getNameByContent(content: string) {
19-
return this.tags.map((tag) => (tag.node.childNodes[0] as HTMLElement).attributes).filter(tag => tag.content && tag.content == content)[0] ? this.tags.map((tag) => (tag.node.childNodes[0] as HTMLElement).attributes).filter(tag => tag.content && tag.content === content)[0].name : null;
19+
return this.tags.map((tag) => (tag.node.childNodes[0] as HTMLElement).attributes).filter(tag => tag.content && tag.content === content)[0] ? this.tags.map((tag) => (tag.node.childNodes[0] as HTMLElement).attributes).filter(tag => tag.content && tag.content === content)[0].name : null;
2020
}
2121
}

src/metatag.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {UrlMetadataParser} from './url-metadata-parser';
2+
import {Metatag} from './metatag';
3+
import {MetaEntity} from './meta.entity';
4+
5+
const metaTags = [
6+
new Metatag('<meta name="author" content="Woo YeongJun"/>'),
7+
new Metatag('<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />'),
8+
new Metatag('<meta property="og:site_name" content="Hax0rs website">')
9+
];
10+
11+
const metaEntity = new MetaEntity(metaTags);
12+
13+
it('Get contents by name.', () => {
14+
expect(metaEntity.getContentByName('author')).toBe('Woo YeongJun');
15+
});
16+
17+
it('Get name by contents.', () => {
18+
expect(metaEntity.getNameByContent('Woo YeongJun')).toBe('author');
19+
});
20+
21+
it('Get contents by property by name.', () => {
22+
expect(metaEntity.getContentByPropertyName('og:site_name')).toBe('Hax0rs website');
23+
});
24+
25+

src/url-metadata-parser.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {concatMap, map} from 'rxjs/operators';
55
import {of} from 'rxjs/internal/observable/of';
66
import {Metatag} from './metatag';
77
import {MetaEntity} from './meta.entity';
8+
import * as iconvLte from 'iconv-lite';
89

9-
const iconvLte = require("iconv-lite");
1010
export type Charset = string;
1111
export type IntermediateResult = Charset | null;
1212
export enum Errors {
@@ -30,14 +30,14 @@ export class UrlMetadataParser {
3030
['bocu-1', [0xFB, 0xEE, 0x28]],
3131
['gb-18030', [0x84, 0x31, 0x95, 0x33]],
3232
].map(([c, bytes]: [string, number[]]) => {
33-
return ([c, Buffer.from(bytes)] as [Charset, Buffer])
33+
return ([c, Buffer.from(bytes)] as [Charset, Buffer]);
3434
}));
3535

3636
const startsWith = (bom) => {
37-
return buf.slice(0, bom.length).equals(bom)
37+
return buf.slice(0, bom.length).equals(bom);
3838
};
3939

40-
for ( let [charset, bom] of boms ) {
40+
for ( const [charset, bom] of boms ) {
4141
if ( startsWith(bom) ) {
4242
return of(charset.toUpperCase());
4343
}
@@ -59,9 +59,9 @@ export class UrlMetadataParser {
5959
}
6060
return body.match(/<meta[^>]+>/g).map(val => new Metatag(val));
6161
})
62-
)
62+
);
6363
}),
6464
map((tags: Metatag[]) => new MetaEntity(tags))
65-
)
65+
);
6666
}
6767
}

tslint.json

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
11
{
22
"defaultSeverity": "error",
3-
"extends": [
4-
"tslint:recommended"
5-
],
6-
"jsRules": {},
7-
"rules": {},
3+
"extends": ["tslint:recommended"],
4+
"jsRules": {
5+
"no-unused-expression": true
6+
},
7+
"rules": {
8+
"eofline": false,
9+
"quotemark": [true, "single"],
10+
"trailing-comma": false,
11+
"member-access": false,
12+
"indent": false,
13+
"ordered-imports": [false],
14+
"no-implicit-dependencies": false,
15+
"no-unnecessary-initializer": false,
16+
"unified-signatures": false,
17+
"callable-types": false,
18+
"max-line-length": [150],
19+
"member-ordering": [false],
20+
"curly": false,
21+
"interface-name": [false],
22+
"array-type": [false],
23+
"no-empty-interface": false,
24+
"no-empty": false,
25+
"arrow-parens": false,
26+
"object-literal-sort-keys": false,
27+
"no-unused-expression": false,
28+
"max-classes-per-file": [false],
29+
"ban-types": false,
30+
"variable-name": [false],
31+
"one-line": [false],
32+
"one-variable-per-declaration": [false],
33+
"no-return-await": true,
34+
"match-default-export-name": true,
35+
"prefer-readonly": true
36+
},
837
"rulesDirectory": []
938
}

0 commit comments

Comments
 (0)