Skip to content

Commit

Permalink
Added unit tests for current functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
candunaj committed Nov 8, 2021
1 parent 64e73aa commit ffbd006
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 4 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"scripts": {
"start": "node runner.js",
"lint": "eslint .",
"test": "mocha --recursive test/acceptance test/unit"
"test": "mocha --recursive test/acceptance test/unit",
"test-watch": "mocha --watch --recursive test/acceptance test/unit"
},
"author": "",
"license": "ISC",
Expand All @@ -26,4 +27,4 @@
"recursive-copy": "^2.0.13",
"temp": "^0.9.4"
}
}
}
69 changes: 69 additions & 0 deletions test/unit/br.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const testCase = require('../helpers/test-case');

describe('nested elements', function() {
testCase({
name: 'Br inside text',
input: `
<span>
Hello
<br />
world
</span>`,
output: `
<span>
{{t "Hello <br /> world" htmlSafe=true}}
</span>`
});

testCase({
name: 'No br at the end of the translation',
input: `
<span>
Hello
<br />
world
<br />
</span>`,
output: `
<span>
{{t "Hello <br /> world" htmlSafe=true}}
<br />
</span>`
});

testCase({
name: 'No br at the start of the translation',
input: `
<span>
<br />
Hello
<br />
world
</span>`,
output: `
<span>
<br />
{{t "Hello <br /> world" htmlSafe=true}}
</span>`
});

testCase({
name: 'No br at the start/end of the translation',
input: `
<span>
<br />
<br />
Hello
<br />
world
<br />
</span>`,
output: `
<span>
<br />
<br />
{{t "Hello <br /> world" htmlSafe=true}}
<br />
</span>`
});
})
31 changes: 31 additions & 0 deletions test/unit/emptyText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const testCase = require('../helpers/test-case');

describe('Empty text', function() {
testCase({
name: 'Remove empty elements from start and begining of a translation',
input: `
<strong></strong>
<strong></strong>
This is
<strong></strong>
`,
output: `
<strong></strong>
<strong></strong>
{{t "This is"}}
<strong></strong>
`
});

testCase({
name: 'Do not remove empty elements inside translation.',
input: `
This is
<strong></strong>
text.
`,
output: `
{{t "This is <strong></strong> text." htmlSafe=true}}
`
});
});
32 changes: 32 additions & 0 deletions test/unit/groupAllowedElements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

const testCase = require('../helpers/test-case');

describe('grouping allowed elements', function() {
testCase({
name: 'merge html elements without element-modifiers',
input: `
<div>
<i>a</i>
<em>b</em>
<b>c</b>
<strong>d</strong>
<bold>e</bold>
<span>f</span>
<a>g</a>
<div>Not Formatting Element</div>
<code>h</code>
<br />
<sup>i</sup>
<sub>j</sub>
</div>
`,
output: `
<div>
{{t "<i>a</i> <em>b</em> <b>c</b> <strong>d</strong> <bold>e</bold> <span>f</span> <a>g</a>" htmlSafe=true}}
<div>{{t "Not Formatting Element"}}</div>
{{t "<code>h</code> <br /> <sup>i</sup> <sub>j</sub>" htmlSafe=true}}
</div>
`
});

});
29 changes: 29 additions & 0 deletions test/unit/i.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const testCase = require('../helpers/test-case');

describe('test of auditboard-icons', function() {
testCase({
name: 'icon not translated',
input: `
<i class="auditboard-icons fs-14 mr-3">lock_fill</i> Changes
`,
output: `
<i class="auditboard-icons fs-14 mr-3">lock_fill</i> {{t "Changes"}}
`
});

testCase({
name: 'i translated',
input: `
<i class="fs-14 mr-3">This is</i> nice text.
`,
output: `
{{t "<i class='fs-14 mr-3'>This is</i> nice text." htmlSafe=true}}
`
});

testCase({
name: 'i translated',
input: `<i class="fs-14 mr-3">This is nice text.</i>`,
output: `<i class="fs-14 mr-3">{{t "This is nice text."}}</i>`
});
});
29 changes: 29 additions & 0 deletions test/unit/modifiers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const testCase = require('../helpers/test-case');

describe('test modifiers', function() {
testCase({
name: 'merge html elements without element-modifiers',
input: `
This is
<strong>interesting</strong>
text.
`,
output: `
{{t "This is <strong>interesting</strong> text." htmlSafe=true}}
`
});

testCase({
name: 'do not merge html elements if they have element-modifiers',
input: `
This is
<strong {{on 'click' this.doSomething}}>interesting</strong>
text.
`,
output: `
{{t "This is"}}
<strong {{on 'click' this.doSomething}}>{{t "interesting"}}</strong>
{{t "text."}}
`
});
});
84 changes: 82 additions & 2 deletions test/unit/nested.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const testCase = require('../helpers/test-case');

describe('nested elements', function () {
describe('nested elements', function() {
testCase({
name: 'nested span',
input: `<img
Expand All @@ -20,5 +20,85 @@ describe('nested elements', function () {
<span>
{{t "aaa <span title='{sponsor_name} website'> test </span>" sponsor_name=sponsor.name htmlSafe=true}}
</span>`
})
});

testCase({
name: 'multiple nested span',
input: `
<span>
<span>
Hello
</span>
<span title="{{sponsor.name}} website">
world
</span>
</span>`,
output: `
<span>
{{t "<span> Hello </span> <span title='{sponsor_name} website'> world </span>" sponsor_name=sponsor.name htmlSafe=true}}
</span>`
});

testCase({
name: 'nested span without text',
input: `
<span>
{{this.a}}
<span>
{{this.b}}
</span>
<span title="{{sponsor.name}} website">
{{this.c}}
</span>
</span>`,
output: `
<span>
{{this.a}}
<span>
{{this.b}}
</span>
<span title={{t "{sponsor_name} website" sponsor_name=sponsor.name}}>
{{this.c}}
</span>
</span>`,
});

testCase({
name: 'nested span with text',
input: `
<span>
Status
{{this.a}}
<strong>
{{this.b}}
</strong>
<span title="{{sponsor.name}} website">
{{this.c}}
</span>
is interesting.
</span>`,
output: `
<span>
{{t "Status {this_a} <strong> {this_b} </strong> <span title='{sponsor_name} website'> {this_c} </span> is interesting." this_a=this.a this_b=this.b sponsor_name=sponsor.name this_c=this.c htmlSafe=true}}
</span>`,
});

testCase({
name: 'no empty space between html tags',
input: `
<span>
Status
{{this.a}}
<strong>
{{this.b}}
</strong><span title="{{sponsor.name}} website">
{{this.c}}
</span>
is interesting.
</span>`,
output: `
<span>
{{t "Status {this_a} <strong> {this_b} </strong><span title='{sponsor_name} website'> {this_c} </span> is interesting." this_a=this.a this_b=this.b sponsor_name=sponsor.name this_c=this.c htmlSafe=true}}
</span>`,
});
})

0 comments on commit ffbd006

Please sign in to comment.