@@ -5,102 +5,222 @@ const fs = require('graceful-fs');
55let EmojiAdd = require ( '../../lib/emoji-add' ) ;
66let EmojiAdminList = require ( '../../lib/emoji-admin-list' ) ;
77let SlackClient = require ( '../../lib/slack-client' ) ;
8+ let FileUtils = require ( '../../lib/util/file-utils' ) ;
89let add = require ( '../../emojme-add' ) . add ;
910
1011let sandbox ;
11- beforeEach ( function ( ) {
12+ beforeEach ( ( ) => {
1213 sandbox = sinon . createSandbox ( ) ;
1314} ) ;
1415
15- afterEach ( function ( ) {
16+ afterEach ( ( ) => {
1617 sandbox . restore ( ) ;
1718} ) ;
1819
1920describe ( 'add' , ( ) => {
20- beforeEach ( function ( ) {
21- let uploadStub = sandbox . stub ( EmojiAdd . prototype , 'upload' ) ;
22- uploadStub . callsFake ( arg1 => Promise . resolve ( { emojiList : arg1 } ) ) ;
21+ context ( 'pre upload configuration' , ( ) => {
22+ beforeEach ( ( ) => {
23+ let uploadStub = sandbox . stub ( EmojiAdd . prototype , 'upload' ) ;
24+ uploadStub . callsFake ( arg1 => Promise . resolve ( { emojiList : arg1 } ) ) ;
2325
24- sandbox . stub ( EmojiAdminList . prototype , 'get' ) . withArgs ( sinon . match . any ) . resolves (
25- [ { name : 'emoji-1' } ]
26- ) ;
27- } ) ;
26+ sandbox . stub ( EmojiAdminList . prototype , 'get' ) . withArgs ( sinon . match . any ) . resolves (
27+ [ { name : 'emoji-1' } ]
28+ ) ;
29+ } ) ;
2830
29- it ( 'renames emoji to avoid collisions when avoidCollisions is set' , ( ) => {
30- let options = {
31- name : [ 'emoji-1' , 'emoji-2' , 'emoji-3' , 'emoji-4' ] ,
32- aliasFor : [ 'emoji' , 'emoji' , 'emoji' , 'emoji' ] ,
33- avoidCollisions : true
34- } ;
35-
36- return add ( 'subdomain' , 'token' , options ) . then ( results => {
37- assert . deepEqual ( results , [
38- {
39- collisions : [ ] ,
40- emojiList : [
41- {
42- "alias_for" : "emoji" ,
43- "is_alias" : 1 ,
44- "name" : "emoji-5" ,
45- "collision" : "emoji-1"
46- } ,
47- {
48- "alias_for" : "emoji" ,
49- "is_alias" : 1 ,
50- "name" : "emoji-2"
51- } ,
52- {
53- "alias_for" : "emoji" ,
54- "is_alias" : 1 ,
55- "name" : "emoji-3"
56- } ,
57- {
58- "alias_for" : "emoji" ,
59- "is_alias" : 1 ,
60- "name" : "emoji-4"
61- } ,
62- ]
63- }
64- ] ) ;
31+ it ( 'renames emoji to avoid collisions when avoidCollisions is set' , ( ) => {
32+ let options = {
33+ name : [ 'emoji-1' , 'emoji-2' , 'emoji-3' , 'emoji-4' ] ,
34+ aliasFor : [ 'emoji' , 'emoji' , 'emoji' , 'emoji' ] ,
35+ avoidCollisions : true
36+ } ;
37+
38+ return add ( 'subdomain' , 'token' , options ) . then ( results => {
39+ assert . deepEqual ( results , [
40+ {
41+ collisions : [ ] ,
42+ emojiList : [
43+ {
44+ "alias_for" : "emoji" ,
45+ "is_alias" : 1 ,
46+ "name" : "emoji-5" ,
47+ "collision" : "emoji-1"
48+ } ,
49+ {
50+ "alias_for" : "emoji" ,
51+ "is_alias" : 1 ,
52+ "name" : "emoji-2"
53+ } ,
54+ {
55+ "alias_for" : "emoji" ,
56+ "is_alias" : 1 ,
57+ "name" : "emoji-3"
58+ } ,
59+ {
60+ "alias_for" : "emoji" ,
61+ "is_alias" : 1 ,
62+ "name" : "emoji-4"
63+ } ,
64+ ]
65+ }
66+ ] ) ;
67+ } ) ;
68+ } ) ;
69+
70+ it ( 'collects and does not attempt to upload collisions when avoidCollisions is false' , ( ) => {
71+ let options = {
72+ name : [ 'emoji-1' , 'emoji-2' , 'emoji-3' , 'emoji-4' ] ,
73+ aliasFor : [ 'emoji' , 'emoji' , 'emoji' , 'emoji' ] ,
74+ avoidCollisions : false
75+ } ;
76+
77+ return add ( 'subdomain' , 'token' , options ) . then ( results => {
78+ assert . deepEqual ( results , [
79+ {
80+ collisions : [
81+ {
82+ "alias_for" : "emoji" ,
83+ "is_alias" : 1 ,
84+ "name" : "emoji-1" ,
85+ } ,
86+ ] ,
87+ emojiList : [
88+ {
89+ "alias_for" : "emoji" ,
90+ "is_alias" : 1 ,
91+ "name" : "emoji-2"
92+ } ,
93+ {
94+ "alias_for" : "emoji" ,
95+ "is_alias" : 1 ,
96+ "name" : "emoji-3"
97+ } ,
98+ {
99+ "alias_for" : "emoji" ,
100+ "is_alias" : 1 ,
101+ "name" : "emoji-4"
102+ } ,
103+ ]
104+ }
105+ ] ) ;
106+ } ) ;
65107 } ) ;
66108 } ) ;
67109
68- it ( 'collects and does not attempt to upload collisions when avoidCollisions is false' , ( ) => {
69- let options = {
70- name : [ 'emoji-1' , 'emoji-2' , 'emoji-3' , 'emoji-4' ] ,
71- aliasFor : [ 'emoji' , 'emoji' , 'emoji' , 'emoji' ] ,
72- avoidCollisions : false
73- } ;
74-
75- return add ( 'subdomain' , 'token' , options ) . then ( results => {
76- assert . deepEqual ( results , [
77- {
78- collisions : [
79- {
80- "alias_for" : "emoji" ,
81- "is_alias" : 1 ,
82- "name" : "emoji-1" ,
83- } ,
84- ] ,
85- emojiList : [
86- {
87- "alias_for" : "emoji" ,
88- "is_alias" : 1 ,
89- "name" : "emoji-2"
90- } ,
91- {
92- "alias_for" : "emoji" ,
93- "is_alias" : 1 ,
94- "name" : "emoji-3"
95- } ,
96- {
97- "alias_for" : "emoji" ,
98- "is_alias" : 1 ,
99- "name" : "emoji-4"
100- } ,
101- ]
102- }
103- ] ) ;
110+ context ( 'upload behavior' , ( ) => {
111+ beforeEach ( ( ) => {
112+ sandbox . stub ( EmojiAdminList . prototype , 'get' ) . withArgs ( sinon . match . any ) . resolves (
113+ [ { name : 'emoji-1' } ]
114+ ) ;
115+ } ) ;
116+
117+ it ( 'returns array of subdomain specific results when uploading aliases' , ( ) => {
118+ let subdomains = [ 'subdomain_1' , 'subdomain_2' ] ;
119+ let tokens = [ 'token_1' , 'token_2' ] ;
120+ let options = {
121+ name : [ 'emoji-1' , 'emoji-2' , 'emoji-3' , 'emoji-4' ] ,
122+ aliasFor : [ 'emoji' , 'emoji' , 'emoji' , 'emoji' ] ,
123+ avoidCollisions : false
124+ } ;
125+
126+ let stub = sandbox . stub ( SlackClient . prototype , 'request' )
127+ stub . withArgs ( sinon . match . any ) . resolves (
128+ { ok : true }
129+ ) ;
130+ stub . withArgs ( sinon . match . any ) . onFirstCall ( ) . resolves (
131+ { ok : false , error : 'an error message' }
132+ ) ;
133+
134+ return add ( subdomains , tokens , options ) . then ( results => {
135+ assert . equal ( results . length , 2 ) ;
136+ assert . equal ( results [ 0 ] . subdomain , 'subdomain_1' ) ;
137+ assert . equal ( results [ 0 ] . emojiList . length , 3 ) ; //4 minus 1 collision
138+ assert . deepEqual ( results [ 0 ] . errorList , [ {
139+ name : 'emoji-2' ,
140+ is_alias : 1 ,
141+ alias_for : 'emoji' ,
142+ error : 'an error message'
143+ } ] ) ; //error on first call
144+
145+ assert . equal ( results [ 0 ] . collisions . length , 1 ) ; //collision with emoji-1
146+
147+ assert . equal ( results [ 1 ] . subdomain , 'subdomain_2' ) ;
148+ assert . equal ( results [ 1 ] . emojiList . length , 3 ) ; //4 minus 1 collision
149+ assert . equal ( results [ 1 ] . errorList . length , 0 ) ; //no errors
150+ assert . equal ( results [ 1 ] . collisions . length , 1 ) ; //collision with emoji-1
151+ } ) ;
152+ } ) ;
153+
154+ it ( 'returns array of subdomain specific results when uploading new emoji' , ( ) => {
155+ let subdomains = [ 'subdomain_1' , 'subdomain_2' ] ;
156+ let tokens = [ 'token_1' , 'token_2' ] ;
157+ let options = {
158+ src : [ 'emoji-1.jpg' , 'emoji-2.jpg' , 'emoji-3.jpg' , 'emoji-4.jpg' ] ,
159+ avoidCollisions : false
160+ } ;
161+
162+ let getDataStub = sandbox . stub ( FileUtils , 'getData' ) . withArgs ( sinon . match . any ) . resolves ( 'emoji data' ) ;
163+ let requestStub = sandbox . stub ( SlackClient . prototype , 'request' )
164+ requestStub . withArgs ( sinon . match . any ) . resolves (
165+ { ok : true }
166+ ) ;
167+ requestStub . withArgs ( sinon . match . any ) . onFirstCall ( ) . resolves (
168+ { ok : false , error : 'an error message' }
169+ ) ;
170+
171+ return add ( subdomains , tokens , options ) . then ( results => {
172+ assert . equal ( results . length , 2 ) ;
173+ assert . equal ( results [ 0 ] . subdomain , 'subdomain_1' ) ;
174+ assert . equal ( results [ 0 ] . emojiList . length , 3 ) ; //4 minus 1 collision
175+ assert . deepEqual ( results [ 0 ] . errorList , [ {
176+ name : 'emoji-2' ,
177+ url : 'emoji-2.jpg' ,
178+ is_alias : 0 ,
179+ error : 'an error message'
180+ } ] ) ; //error on first call
181+
182+ assert . equal ( results [ 0 ] . collisions . length , 1 ) ; //collision with emoji-1
183+
184+ assert . equal ( results [ 1 ] . subdomain , 'subdomain_2' ) ;
185+ assert . equal ( results [ 1 ] . emojiList . length , 3 ) ; //4 minus 1 collision
186+ assert . equal ( results [ 1 ] . errorList . length , 0 ) ; //no errors
187+ assert . equal ( results [ 1 ] . collisions . length , 1 ) ; //collision with emoji-1
188+ } ) ;
189+ } ) ;
190+
191+ it ( 'allows mixed new / alias inputs when correctly formatted' , ( ) => {
192+ let options = {
193+ src : [ 'new-emoji-1.jpg' , null , null , 'new-emoji-4.gif' ] ,
194+ name : [ null , 'alias-name-2' , 'alias-name-3' , 'emoji-name-4' ] ,
195+ aliasFor : [ 'alias-src-2' , 'alias-src-3' ] ,
196+ } ;
197+
198+ let uploadStub = sandbox . stub ( EmojiAdd . prototype , 'upload' ) ;
199+ uploadStub . callsFake ( emojiToUpload => {
200+ assert . deepEqual ( emojiToUpload , [
201+ { is_alias : 0 , url : 'new-emoji-1.jpg' , name : 'new-emoji-1' } ,
202+ { is_alias : 1 , alias_for : 'alias-src-2' , name : 'alias-name-2' } ,
203+ { is_alias : 1 , alias_for : 'alias-src-3' , name : 'alias-name-3' } ,
204+ { is_alias : 0 , url : 'new-emoji-4.gif' , name : 'emoji-name-4' }
205+ ] ) ;
206+ return Promise . resolve ( { } ) ;
207+ } ) ;
208+
209+ return add ( 'subdomain' , 'tokens' , options ) ;
210+ } ) ;
211+
212+ it ( 'rejects poorly formatted inputs' , ( ) => {
213+ let options = {
214+ src : [ 'emoji-1.jpg' ] ,
215+ name : [ 'emoji-1' , 'emoji-2' ] ,
216+ aliasFor : [ 'emoji-2-original' , 'unattached-alias' ] ,
217+ } ;
218+
219+ return add ( 'subdomain' , 'tokens' , options )
220+ . then ( ( ) => { fail ( ) ; } )
221+ . catch ( err => {
222+ assert . equal ( err , 'Invalid input. Either not all inputs have been consumed, or not all emoji are well formed. Consider simplifying input, or padding input with `null` values.' ) ;
223+ } ) ;
104224 } ) ;
105225 } ) ;
106226} ) ;
0 commit comments