-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.js
112 lines (89 loc) · 3.68 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
const sut = require('./index.js');
describe('#default args', () => {
let parse;
beforeEach(() => {
parse = sut();
});
it('Should replace digits', () => {
expect(parse('/url')).toEqual('/url');
expect(parse('/url/12345')).toEqual('/url/*');
expect(parse('/url/12345/')).toEqual('/url/*/');
expect(parse('/url/12345/abc')).toEqual('/url/*/abc');
expect(parse('/url/12345/abc/')).toEqual('/url/*/abc/');
});
it('Should replace UUIDs', () => {
expect(parse('/url')).toEqual('/url');
expect(parse('/url/123e4567-e89b-12d3-a456-426655440000')).toEqual('/url/*');
expect(parse('/url/123e4567-e89b-12d3-a456-426655440000/abc')).toEqual('/url/*/abc');
});
});
describe('#custom matchers', () => {
it('Should test upper-cased hex strings', () => {
const parse = sut({
matchers: [sut.getBuiltInMatchers().hexUppercase]
});
expect(parse('/url')).toEqual('/url');
expect(parse('/url/DEADBEEF')).toEqual('/url/*');
expect(parse('/url/DEADBEEF/abc')).toEqual('/url/*/abc');
expect(parse('/url/deadbeef/abc')).toEqual('/url/deadbeef/abc');
});
it('Should test lower-cased hex strings', () => {
const parse = sut({
matchers: [sut.getBuiltInMatchers().hexLowercase]
});
expect(parse('/url')).toEqual('/url');
expect(parse('/url/deadbeef')).toEqual('/url/*');
expect(parse('/url/deadbeef/abc')).toEqual('/url/*/abc');
expect(parse('/url/DEADBEEF/abc')).toEqual('/url/DEADBEEF/abc');
});
it('Should test ISO8061 timestamp strings', () => {
const parse = sut({
matchers: [sut.getBuiltInMatchers().iso8061]
});
expect(parse('/url')).toEqual('/url');
expect(parse('/url/2015/abc')).toEqual('/url/*/abc');
expect(parse('/url/2015-06/abc')).toEqual('/url/*/abc');
expect(parse('/url/2015-06-01/abc')).toEqual('/url/*/abc');
expect(parse('/url/2015-06-01T12:00:00Z/abc')).toEqual('/url/*/abc');
expect(parse('/url/2015-06-01T12:00:00-04:00/abc')).toEqual('/url/*/abc');
expect(parse('/url/2015-06-01T12:00:00.000+04:00/abc')).toEqual('/url/*/abc');
});
it('Should test an arbitrary regex', () => {
const parse = require('url-id-replace')({
matchers: [/^ITEM-\d*$/]
});
expect(parse('/url')).toEqual('/url');
expect(parse('/url/ITEM-123')).toEqual('/url/*');
expect(parse('/url/ITEM-123/abc')).toEqual('/url/*/abc');
});
});
describe('#custom placeholder', () => {
it('Should replace matches with the given string', () => {
const parse = sut({
placeholder: 'X'
});
expect(parse('/url')).toEqual('/url');
expect(parse('/url/12345')).toEqual('/url/X');
expect(parse('/url/12345/')).toEqual('/url/X/');
expect(parse('/url/12345/abc')).toEqual('/url/X/abc');
expect(parse('/url/12345/abc/')).toEqual('/url/X/abc/');
});
});
describe('#getBuiltInMatchers', () => {
it('Should get all the built-in masks', () => {
const matchers = sut.getBuiltInMatchers();
expect(matchers.digits).toBeInstanceOf(RegExp);
expect(matchers.hexLowercase).toBeInstanceOf(RegExp);
});
it('Should return a new copy of the masks each time', () => {
const matchers = sut.getBuiltInMatchers();
matchers.digits = 'garbage';
expect(sut.getBuiltInMatchers().digits).toBeInstanceOf(RegExp); // Should be unchanged
});
});
it('Should throw an error if an unknown option is passed', () => {
expect(() => sut({
something: true
})).toThrowError();
});