Skip to content

Commit 7958568

Browse files
Add more integration testing (#8)
* why isn't sandbox workingggg * clean up emojme download spec and save behavior * fixup userstats * one more test and sketches for the next file
1 parent e5c16fd commit 7958568

File tree

8 files changed

+143
-13
lines changed

8 files changed

+143
-13
lines changed

emojme-download.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ async function download(subdomains, tokens, options) {
3434
let [authPairs] = Helpers.zipAuthPairs(subdomains, tokens);
3535

3636
let downloadPromises = authPairs.map(async authPair => {
37+
let [subdomain, token] = authPair;
38+
let saveResults;
39+
3740
let adminList = new EmojiAdminList(...authPair, options.output);
3841
let emojiList = await adminList.get(options.bustCache);
3942
if (options.save && options.save.length) {
40-
return await EmojiAdminList.save(emojiList, authPair[0], options.save);
43+
saveResults = await EmojiAdminList.save(emojiList, subdomain, options.save);
4144
}
4245

43-
return {emojiList: emojiList, subdomain: authPair[1]};
46+
return {emojiList: emojiList, subdomain: subdomain, saveResults: saveResults};
4447
});
4548

4649
return Helpers.formatResultsHash(await Promise.all(downloadPromises));

emojme-user-stats.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ async function userStats(subdomains, tokens, options) {
3939
let emojiList = await emojiAdminList.get(options.bustCache);
4040
if (users && users.length > 0) {
4141
let results = EmojiAdminList.summarizeUser(emojiList, authPair[0], users)
42-
results.forEach(result => {
42+
return results.map(result => {
4343
let safeUserName = result.user.toLowerCase().replace(/ /g, '-');
4444
FileUtils.writeJson(`./build/${safeUserName}.${result.subdomain}.adminList.json`, result.userEmoji, null, 3);
45-
return results;
45+
debugger;
46+
return {subdomain: authPair[0], userStatsResults: results};
4647
});
4748
} else {
4849
let results = EmojiAdminList.summarizeSubdomain(emojiList, authPair[0], options.top)
@@ -51,7 +52,7 @@ async function userStats(subdomains, tokens, options) {
5152
FileUtils.writeJson(`./build/${safeUserName}.${result.subdomain}.adminList.json`, result.userEmoji, null, 3);
5253
});
5354

54-
return results;
55+
return {subdomain: authPair[0], userStatsResults: results};
5556
}
5657
});
5758

lib/emoji-admin-list.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ class EmojiAdminList {
122122
.value();
123123
console.log(`The top ${n} contributors for ${subdomain}'s ${emojiList.length} emoji are:`);
124124
let topUsers = sortedUsers.slice(0, n);
125-
debugger;
126125
return this.summarizeUser(emojiList, subdomain, topUsers.map(e => e.user));
127126
}
128127

@@ -160,7 +159,7 @@ class EmojiAdminList {
160159
groupedEmoji[user].forEach(emoji => {
161160
try {
162161
let fileType;
163-
if (emoji.url.match(/^http.*/)) {
162+
if (emoji.url.match(/^.*\.(gif|png|jpg|jpg)$/)) {
164163
fileType = emoji.url.split('.').slice(-1);
165164
} else if (path.match(/^data:.*/)) {
166165
fileType = emoji.url.match(/^data:image\/(gif|png|jpg|jpeg).*/)
@@ -171,7 +170,7 @@ class EmojiAdminList {
171170

172171
promiseArray.push(FileUtils.getData(emoji.url)
173172
.then(emojiData => FileUtils.saveData(emojiData, path))
174-
.then(() => process.stdout.write('.'))
173+
.then(() => { process.stdout.write('.'); return path; })
175174
)
176175
} catch (err) {
177176
// There is a bizarre case where you can make an alias for a default emoji
@@ -182,7 +181,7 @@ class EmojiAdminList {
182181
});
183182
});
184183

185-
return {results: await Promise.all(promiseArray), emojiList: emojiList, subdomain: subdomain};
184+
return await Promise.all(promiseArray);
186185
}
187186
}
188187

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"scripts": {
1616
"test": "mocha spec/unit/**/* && mocha spec/integration/**/*",
1717
"test:unit": "mocha spec/unit/**/*",
18-
"test:integration": "mocha spec/integration/**/*"
18+
"test:integration": "mocha spec/integration/**/*",
19+
"test:debug": "node inspect node_modules/mocha/bin/_mocha"
1920
},
2021
"license": "ISC",
2122
"dependencies": {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const assert = require('chai').assert;
2+
const sinon = require('sinon');
3+
const fs = require('graceful-fs');
4+
5+
let specHelper = require('../spec-helper');
6+
let EmojiAdd = require('../../lib/emoji-add');
7+
let EmojiAdminList = require('../../lib/emoji-admin-list');
8+
let SlackClient = require('../../lib/slack-client');
9+
let FileUtils = require('../../lib/util/file-utils');
10+
let download = require('../../emojme-download').download;
11+
12+
let sandbox;
13+
let getStub;
14+
let saveDataStub;
15+
16+
beforeEach(() => {
17+
sandbox = sinon.createSandbox();
18+
});
19+
20+
afterEach(() => {
21+
sandbox.restore();
22+
});
23+
24+
describe('download', () => {
25+
let subdomains = ['subdomain1', 'subdomain2'];
26+
let tokens = ['token1', 'token2'];
27+
28+
beforeEach(() => {
29+
getStub = sandbox.stub(EmojiAdminList.prototype, 'get');
30+
getStub.resolves(
31+
specHelper.testEmojiList(10)
32+
);
33+
34+
// prevent writing during tests
35+
sandbox.stub(FileUtils, 'saveData').callsFake((arg1, arg2) => Promise.resolve(arg2));
36+
sandbox.stub(FileUtils, 'mkdirp');
37+
});
38+
39+
it('downloads emojiList when save is not set', () => {
40+
return download(subdomains, tokens).then(results => {
41+
assert.deepEqual(results.subdomain1.emojiList, specHelper.testEmojiList(10));
42+
assert.deepEqual(results.subdomain2.emojiList, specHelper.testEmojiList(10));
43+
44+
assert.deepEqual(results.subdomain1.saveResults, undefined);
45+
assert.deepEqual(results.subdomain2.saveResults, undefined);
46+
});
47+
});
48+
49+
it('downloads emoji for specified users when save is set', () => {
50+
return download(subdomains, tokens, {save: ['test-user-1', 'test-user-0']}).then(results => {
51+
assert.deepEqual(results.subdomain1.emojiList, specHelper.testEmojiList(10));
52+
assert.deepEqual(results.subdomain2.emojiList, specHelper.testEmojiList(10));
53+
54+
assert.equal(results.subdomain1.saveResults.length, 10);
55+
assert.equal(results.subdomain2.saveResults.length, 10);
56+
});
57+
});
58+
});

spec/integration/emojme-upload-spec.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const assert = require('chai').assert;
1+
const chai = require('chai');
2+
const assert = chai.assert;
3+
24
const sinon = require('sinon');
35
const fs = require('graceful-fs');
46

@@ -8,6 +10,8 @@ let SlackClient = require('../../lib/slack-client');
810
let upload = require('../../emojme-upload').upload;
911

1012
let sandbox;
13+
let uploadStub;
14+
1115
beforeEach(function () {
1216
sandbox = sinon.createSandbox();
1317
});
@@ -18,14 +22,42 @@ afterEach(function () {
1822

1923
describe('upload', () => {
2024
beforeEach(function () {
21-
let uploadStub = sandbox.stub(EmojiAdd.prototype, 'upload');
25+
uploadStub = sandbox.stub(EmojiAdd.prototype, 'upload');
2226
uploadStub.callsFake(arg1 => Promise.resolve({subdomain: 'subdomain', emojiList: arg1}));
2327

2428
sandbox.stub(EmojiAdminList.prototype, 'get').withArgs(sinon.match.any).resolves(
2529
[{ name: 'emoji-1' }]
2630
);
2731
});
2832

33+
it('uploads emoji from specified json', () => {
34+
let options = { src: './spec/fixtures/emojiList.json' };
35+
let fixture = JSON.parse(fs.readFileSync('./spec/fixtures/emojiList.json', 'utf-8'));
36+
37+
return upload('subdomain', 'token', options).then(results => {
38+
assert.deepEqual(results, { subdomain:
39+
{
40+
collisions: [
41+
fixture[0]
42+
],
43+
emojiList: [
44+
fixture[1],
45+
fixture[2],
46+
fixture[3],
47+
]
48+
}
49+
});
50+
51+
assert.deepEqual(uploadStub.getCall(0).args, [
52+
[
53+
fixture[1],
54+
fixture[2],
55+
fixture[3],
56+
]
57+
]);
58+
});
59+
});
60+
2961
it('renames emoji to avoid collisions when avoidCollisions is set', () => {
3062
let options = {
3163
src: './spec/fixtures/emojiList.json',
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const assert = require('chai').assert;
2+
const sinon = require('sinon');
3+
const fs = require('graceful-fs');
4+
5+
let specHelper = require('../spec-helper');
6+
let EmojiAdd = require('../../lib/emoji-add');
7+
let EmojiAdminList = require('../../lib/emoji-admin-list');
8+
let SlackClient = require('../../lib/slack-client');
9+
let FileUtils = require('../../lib/util/file-utils');
10+
let download = require('../../emojme-download').download;
11+
12+
let sandbox;
13+
14+
beforeEach(() => {
15+
sandbox = sinon.createSandbox();
16+
});
17+
18+
afterEach(() => {
19+
sandbox.restore();
20+
});
21+
22+
describe('user-stats', () => {
23+
let subdomains = ['subdomain1', 'subdomain2'];
24+
let tokens = ['token1', 'token2'];
25+
26+
beforeEach(() => {
27+
getStub = sandbox.stub(EmojiAdminList.prototype, 'get');
28+
getStub.resolves(
29+
specHelper.testEmojiList(10)
30+
);
31+
32+
// prevent writing during tests
33+
sandbox.stub(FileUtils, 'saveData').callsFake((arg1, arg2) => Promise.resolve(arg2));
34+
sandbox.stub(FileUtils, 'mkdirp');
35+
});
36+
});

0 commit comments

Comments
 (0)