This repository was archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepost-guard-tests.js
102 lines (81 loc) · 2.78 KB
/
repost-guard-tests.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
'use strict';
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var runner = require('./lib/runner');
var test = runner.test.bind(runner);
var createRepostGuard = require('../src/repost-guard');
runner.subject('createRepostGuard');
var fixtureLines = [
'medium.com/some-user/some-post',
'google.com/some-page',
'yahoo.com/some-page'
];
var fixtureData = fixtureLines.join('\n') + '\n';
var guard;
runner.beforeEach(function() {
guard = createRepostGuard({
directory: path.join(__dirname, 'tmp'),
feedPageSize: 2,
lineLimit: 4,
sync: true
});
guard.setUpWithData = function() {
this.setUp();
this.setData(fixtureData);
this.dataChanged = true;
return this;
};
});
runner.afterEach(function() {
fs.unlinkSync(guard.storeFile());
});
runner.subject('#checkLink');
test('adds normalized link to data if unique', function() {
guard.setUpWithData();
assert(guard.checkLink('http://twitter.com/me?q=1#id'), 'unique');
assert(guard.checkLink('http://twitter.com/me?q=1#id'), 'still in page');
assert.equal(
guard.data.indexOf('twitter.com/me\n'),
guard.data.lastIndexOf('twitter.com/me\n'),
'appends link only once'
);
});
test("any existing link outside of 'current page' is a repost", function() {
guard.setUpWithData();
assert(guard.checkLink('http://twitter.com/me?q=1#id'), 'unique');
assert(guard.checkLink('http://twitter.com/me?q=1#id'), 'still in page');
assert(guard.checkLink('http://vine.co/playlists/foo'), 'unique');
assert(!guard.checkLink('http://twitter.com/me?q=1#id'), 'repost');
});
test('removes lines from top when line-limit reached ', function() {
guard.setUpWithData();
assert(guard.checkLink('http://twitter.com/hashtag/foo'), 'returns success');
assert(guard.checkLink('http://vine.co/playlists/foo'), 'returns success');
assert.equal(guard.data.indexOf(fixtureLines[0]), -1, 'removes first link');
});
test('works with no initial data', function() {
guard.setUp();
assert(guard.checkLink('http://twitter.com/me?q=1#id'), 'returns success');
});
runner.subject('#setUp');
test('creates store file if none exist', function() {
assert.doesNotThrow(function() { guard.setUp(); });
assert.equal(guard.data, '', 'sets data to blank');
});
test('loads data from store file', function() {
guard.setUpWithData().tearDown();
guard.setUp();
assert.equal(guard.data, fixtureData, 'loads fixture data');
});
runner.subject('#tearDown');
test('flushes data into store file', function() {
var addition = 'twitter.com/hashtag/foo\n';
guard.setUpWithData();
guard.setData(guard.data + addition);
guard.tearDown();
assert.equal(guard.data, null, 'sets data to null');
guard.setUp();
assert.equal(guard.data, fixtureData + addition, 'file has data');
});
runner.report();