Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: remoting/webapp/unittests/l10n_unittest.js

Issue 1002553002: Refactor sinon helper to make it easier to use. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function() { 5 (function() {
6 6
7 'use strict'; 7 'use strict';
8 8
9 module('l10n', { 9 module('l10n', {
10 setup: function() { 10 setup: function() {
11 sinon.$setupStub(chrome.i18n, 'getMessage'); 11 sinon.stub(chrome.i18n, 'getMessage');
12 }, 12 },
13 teardown: function() { 13 teardown: function() {
14 chrome.i18n.getMessage.$testStub.restore(); 14 $testStub(chrome.i18n.getMessage).restore();
15 } 15 }
16 }); 16 });
17 17
18 test('getTranslationOrError(tag) should return tag on error', function() { 18 test('getTranslationOrError(tag) should return tag on error', function() {
19 var translation = l10n.getTranslationOrError('non_existent_tag'); 19 var translation = l10n.getTranslationOrError('non_existent_tag');
20 equal(translation, 'non_existent_tag'); 20 equal(translation, 'non_existent_tag');
21 }); 21 });
22 22
23 test('localizeElementFromTag() should replace innerText by default', 23 test('localizeElementFromTag() should replace innerText by default',
24 function() { 24 function() {
25 var element = document.createElement('div'); 25 var element = document.createElement('div');
26 chrome.i18n.getMessage.$testStub.withArgs('tag') 26 $testStub(chrome.i18n.getMessage).withArgs('tag')
27 .returns('<b>Hello World</b>'); 27 .returns('<b>Hello World</b>');
28 28
29 l10n.localizeElementFromTag(element, 'tag'); 29 l10n.localizeElementFromTag(element, 'tag');
30 30
31 equal(element.innerHTML, '&lt;b&gt;Hello World&lt;/b&gt;'); 31 equal(element.innerHTML, '&lt;b&gt;Hello World&lt;/b&gt;');
32 }); 32 });
33 33
34 test('localizeElementFromTag() should replace innerHTML if flag is set', 34 test('localizeElementFromTag() should replace innerHTML if flag is set',
35 function() { 35 function() {
36 var element = document.createElement('div'); 36 var element = document.createElement('div');
37 chrome.i18n.getMessage.$testStub.withArgs('tag') 37 $testStub(chrome.i18n.getMessage).withArgs('tag')
38 .returns('<b>Hello World</b>'); 38 .returns('<b>Hello World</b>');
39 39
40 l10n.localizeElementFromTag(element, 'tag', null, true); 40 l10n.localizeElementFromTag(element, 'tag', null, true);
41 41
42 equal(element.innerHTML, '<b>Hello World</b>'); 42 equal(element.innerHTML, '<b>Hello World</b>');
43 }); 43 });
44 44
45 test( 45 test(
46 'localizeElement() should replace innerText using the "i18n-content" ' + 46 'localizeElement() should replace innerText using the "i18n-content" ' +
47 'attribute as the tag', 47 'attribute as the tag',
48 function() { 48 function() {
49 var element = document.createElement('div'); 49 var element = document.createElement('div');
50 element.setAttribute('i18n-content', 'tag'); 50 element.setAttribute('i18n-content', 'tag');
51 chrome.i18n.getMessage.$testStub.withArgs('tag') 51 $testStub(chrome.i18n.getMessage).withArgs('tag')
52 .returns('<b>Hello World</b>'); 52 .returns('<b>Hello World</b>');
53 53
54 l10n.localizeElement(element); 54 l10n.localizeElement(element);
55 55
56 equal(element.innerHTML, '&lt;b&gt;Hello World&lt;/b&gt;'); 56 equal(element.innerHTML, '&lt;b&gt;Hello World&lt;/b&gt;');
57 }); 57 });
58 58
59 test( 59 test(
60 'localize() should replace element title using the "i18n-title" ' + 60 'localize() should replace element title using the "i18n-title" ' +
61 'attribute as the tag', 61 'attribute as the tag',
62 function() { 62 function() {
63 var fixture = document.getElementById('qunit-fixture'); 63 var fixture = document.getElementById('qunit-fixture');
64 fixture.innerHTML = '<div class="target" i18n-title="tag"></div>'; 64 fixture.innerHTML = '<div class="target" i18n-title="tag"></div>';
65 chrome.i18n.getMessage.$testStub.withArgs('tag').returns('localized title'); 65 $testStub(chrome.i18n.getMessage)
66 .withArgs('tag')
67 .returns('localized title');
66 68
67 l10n.localize(); 69 l10n.localize();
68 70
69 var target = document.querySelector('.target'); 71 var target = document.querySelector('.target');
70 equal(target.title, 'localized title'); 72 equal(target.title, 'localized title');
71 }); 73 });
72 74
73 test('localize() should support string substitutions', function() { 75 test('localize() should support string substitutions', function() {
74 var fixture = document.getElementById('qunit-fixture'); 76 var fixture = document.getElementById('qunit-fixture');
75 fixture.innerHTML = 77 fixture.innerHTML =
76 '<div class="target" ' + 78 '<div class="target" ' +
77 'i18n-content="tag" ' + 79 'i18n-content="tag" ' +
78 'i18n-value-1="param1" ' + 80 'i18n-value-1="param1" ' +
79 'i18n-value-2="param2">' + 81 'i18n-value-2="param2">' +
80 '</div>'; 82 '</div>';
81 83
82 chrome.i18n.getMessage.$testStub.withArgs('tag', ['param1', 'param2']) 84 $testStub(chrome.i18n.getMessage).withArgs('tag', ['param1', 'param2'])
83 .returns('localized'); 85 .returns('localized');
84 86
85 l10n.localize(); 87 l10n.localize();
86 88
87 var target = document.querySelector('.target'); 89 var target = document.querySelector('.target');
88 equal(target.innerText, 'localized'); 90 equal(target.innerText, 'localized');
89 }); 91 });
90 92
91 test('localize() should support tag substitutions', function() { 93 test('localize() should support tag substitutions', function() {
92 var fixture = document.getElementById('qunit-fixture'); 94 var fixture = document.getElementById('qunit-fixture');
93 fixture.innerHTML = 95 fixture.innerHTML =
94 '<div class="target" i18n-content="tag"' + 96 '<div class="target" i18n-content="tag"' +
95 ' i18n-value-name-1="tag1" i18n-value-name-2="tag2"></div>'; 97 ' i18n-value-name-1="tag1" i18n-value-name-2="tag2"></div>';
96 98
97 var getMessage = chrome.i18n.getMessage.$testStub; 99 $testStub(chrome.i18n.getMessage).withArgs('tag1').returns('param1');
98 getMessage.withArgs('tag1').returns('param1'); 100 $testStub(chrome.i18n.getMessage).withArgs('tag2').returns('param2');
99 getMessage.withArgs('tag2').returns('param2'); 101 $testStub(chrome.i18n.getMessage)
100 getMessage.withArgs('tag', ['param1', 'param2']).returns('localized'); 102 .withArgs('tag', ['param1', 'param2'])
103 .returns('localized');
101 104
102 l10n.localize(); 105 l10n.localize();
103 106
104 var target = document.querySelector('.target'); 107 var target = document.querySelector('.target');
105 equal(target.innerText, 'localized'); 108 equal(target.innerText, 'localized');
106 }); 109 });
107 110
108 })(); 111 })();
OLDNEW
« no previous file with comments | « remoting/webapp/unittests/host_table_entry_unittest.js ('k') | remoting/webapp/unittests/sinon_helpers.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698