@@ -22,45 +22,57 @@ const run = require(`../../utils`).run;
22
22
const cwd = path . join ( __dirname , `..` ) ;
23
23
const cmd = `node translate.js` ;
24
24
const text = `Hello world!` ;
25
+ const text2 = `Goodbye!` ;
25
26
const toLang = `ru` ;
26
27
27
28
describe ( `translate:translate` , ( ) => {
28
- const translate = Translate ( {
29
- key : process . env . TRANSLATE_API_KEY
30
- } ) ;
31
- if ( ! process . env . TRANSLATE_API_KEY ) {
32
- process . stdout . write ( `Skipping Translate API tests...\n` ) ;
33
- return ;
34
- }
29
+ const translate = Translate ( ) ;
35
30
36
- it ( `should detect language` , ( done ) => {
31
+ it ( `should detect language of a single string ` , ( ) => {
37
32
const output = run ( `${ cmd } detect "${ text } "` , cwd ) ;
38
- translate . detect ( text , ( err , result ) => {
39
- assert . ifError ( err ) ;
40
- assert . equal ( output , `Detected: ${ JSON . stringify ( result ) } ` ) ;
41
- done ( ) ;
42
- } ) ;
33
+ return translate . detect ( text )
34
+ . then ( ( results ) => {
35
+ const expected = `Detections:\n${ text } => ${ results [ 0 ] . language } ` ;
36
+ assert . equal ( output , expected ) ;
37
+ } ) ;
38
+ } ) ;
39
+
40
+ it ( `should detect language of multiple strings` , ( ) => {
41
+ const output = run ( `${ cmd } detect "${ text } " "${ text2 } "` , cwd ) ;
42
+ return translate . detect ( [ text , text2 ] )
43
+ . then ( ( results ) => {
44
+ const expected = `Detections:\n${ text } => ${ results [ 0 ] [ 0 ] . language } \n${ text2 } => ${ results [ 0 ] [ 1 ] . language } ` ;
45
+ assert . equal ( output , expected ) ;
46
+ } ) ;
43
47
} ) ;
44
48
45
49
it ( `should list languages` , ( ) => {
46
50
const output = run ( `${ cmd } list` , cwd ) ;
47
- assert . notEqual ( output . indexOf ( `Languages:` ) , - 1 ) ;
48
- assert . notEqual ( output . indexOf ( `{ code: 'af', name: 'Afrikaans' }` ) , - 1 ) ;
51
+ assert . equal ( output . includes ( `Languages:` ) , true ) ;
52
+ assert . equal ( output . includes ( `{ code: 'af', name: 'Afrikaans' }` ) , true ) ;
49
53
} ) ;
50
54
51
55
it ( `should list languages with a target` , ( ) => {
52
56
const output = run ( `${ cmd } list es` , cwd ) ;
53
- assert . notEqual ( output . indexOf ( `Languages:` ) , - 1 ) ;
54
- assert . notEqual ( output . indexOf ( `{ code: 'af', name: 'afrikáans' }` ) , - 1 ) ;
57
+ assert . equal ( output . includes ( `Languages:` ) , true ) ;
58
+ assert . equal ( output . includes ( `{ code: 'af', name: 'afrikáans' }` ) , true ) ;
55
59
} ) ;
56
60
57
- it ( `should translate text ` , ( done ) => {
61
+ it ( `should translate a single string ` , ( ) => {
58
62
const output = run ( `${ cmd } translate ${ toLang } "${ text } "` , cwd ) ;
59
- translate . translate ( text , toLang , ( err , translation ) => {
60
- assert . ifError ( err ) ;
61
- assert . notEqual ( output . indexOf ( `Text: ["${ text } "]` ) , - 1 ) ;
62
- assert . notEqual ( output . indexOf ( `Translation: "${ translation } "` ) , - 1 ) ;
63
- done ( ) ;
64
- } ) ;
63
+ return translate . translate ( text , toLang )
64
+ . then ( ( results ) => {
65
+ const expected = `Translations:\n${ text } => (${ toLang } ) ${ results [ 0 ] } ` ;
66
+ assert . equal ( output , expected ) ;
67
+ } ) ;
68
+ } ) ;
69
+
70
+ it ( `should translate multiple strings` , ( ) => {
71
+ const output = run ( `${ cmd } translate ${ toLang } "${ text } " "${ text2 } "` , cwd ) ;
72
+ return translate . translate ( [ text , text2 ] , toLang )
73
+ . then ( ( results ) => {
74
+ const expected = `Translations:\n${ text } => (${ toLang } ) ${ results [ 0 ] [ 0 ] } \n${ text2 } => (${ toLang } ) ${ results [ 0 ] [ 1 ] } ` ;
75
+ assert . equal ( output , expected ) ;
76
+ } ) ;
65
77
} ) ;
66
78
} ) ;
0 commit comments