UNPKG

2.96 kBPlain TextView Raw
1import { assert } from "chai"
2import * as stringExtractor from "../src/extractor/stringExtractor"
3
4describe("stringExtractor", function () {
5 this.timeout(20000)
6 describe("findInJs", function () {
7 it("finds strings", function () {
8 const code = `\
9var func = function() {
10 var x = 5;
11 console.log(T("test1"));
12 console.log(T('test2'));
13 console.log(T('test"quote'));
14 console.log(T("test\\"quote2"));
15}\
16`
17 return assert.deepEqual(stringExtractor.findInJs(code), ["test1", "test2", 'test"quote', 'test"quote2'])
18 })
19
20 it("ignores non-strings", function () {
21 const code = `\
22var func = function() {
23 var x = 5;
24 console.log(T(3));
25 console.log(T(x));
26 console.log(T("test"));
27}\
28`
29 return assert.deepEqual(stringExtractor.findInJs(code), ["test"])
30 })
31
32 return it("finds T calls on objects", function () {
33 const code = `\
34var func = function() {
35 var ctx = {};
36 var obj = {};
37 console.log(ctx.T("test1"));
38 console.log(obj.ctx.T("test2"));
39 console.log(this.ctx.T("test3"));
40}\
41`
42 return assert.deepEqual(stringExtractor.findInJs(code), ["test1", "test2", "test3"])
43 })
44 })
45
46 describe("findInHbs", function () {
47 it("finds strings", function () {
48 const code = `\
49{{T 'some string'}}
50T 'not this'
51{{T "another string"}}\
52`
53 return assert.deepEqual(stringExtractor.findInHbs(code), ["some string", "another string"])
54 })
55
56 it("finds strings in clauses", function () {
57 const code = `\
58{{#if x}}
59 {{T 'some string'}}
60 {{#each y}}
61 {{T "another string"}}
62 {{/each}}
63{{/if}}\
64`
65 return assert.deepEqual(stringExtractor.findInHbs(code), ["some string", "another string"])
66 })
67
68 it("allows empty clauses", function () {
69 const code = `\
70{{#if x}}{{/if}}
71{{#each y}}{{/each}}\
72`
73 return assert.deepEqual(stringExtractor.findInHbs(code), [])
74 })
75
76 return it("finds strings in else clauses", function () {
77 const code = `\
78{{#if x}}
79 {{T 'some string'}}
80{{else}}
81 {{T "another string"}}
82{{/if}}\
83`
84 return assert.deepEqual(stringExtractor.findInHbs(code), ["some string", "another string"])
85 })
86 })
87
88 describe("findInTs", function () {
89 it("finds strings", function () {
90 const code = `\
91export default function(x: any) {
92 window.T("SDFSDF")
93 return x
94}\
95`
96 return assert.deepEqual(stringExtractor.findInTs(code), ["SDFSDF"])
97 })
98
99 return it("finds strings in tsx", function () {
100 const code = `\
101class Chip extends React.Component<any, any> {
102 render() {
103 return (
104 <div className="chip">{T("SDFSDF")}</div>
105 );
106 }
107}\
108`
109 return assert.deepEqual(stringExtractor.findInTsx(code), ["SDFSDF"])
110 })
111 })
112
113 return describe("findFromRoot", () =>
114 it("finds in coffee and hbs", (done) =>
115 stringExtractor.findFromRootDirs([__dirname + "/requireSample"], (strings: any) => {
116 assert.deepEqual(strings.sort(), ["a", "b", "c", "d"])
117 return done()
118 })))
119})