Skip to content

Commit 6602c96

Browse files
committed
Merge pull request mixi-inc#6 from mixi-inc/impl-stage4
Impl stage4
2 parents ac673a2 + b8d362a commit 6602c96

File tree

9 files changed

+308
-12
lines changed

9 files changed

+308
-12
lines changed

gulpfile.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,22 @@ var tasks = [
4444
tasks.forEach(function(task) {
4545
var run = require('gulp-run');
4646

47-
gulp.task(task.cmd, task.help, ['lint'], function() {
47+
gulp.task(task.cmd, task.help, ['lint-' + task.cmd], function() {
4848
// We expected that mocha-phantomjs print colorized results, but it isn't.
4949
// So, I take a fast way that is using gulp-run.
5050
return run('`npm bin`/mocha-phantomjs ' + task.url + ' || true').exec();
5151
});
5252
});
5353

5454

55-
gulp.task('lint', 'ミスのおこりやすいコード・可読性の低いコードがないか検査します', function() {
56-
var eslint = require('gulp-eslint');
55+
tasks.forEach(function(task) {
56+
gulp.task('lint-' + task.cmd, 'ミスのおこりやすいコード・可読性の低いコードがないか検査します', function() {
57+
var eslint = require('gulp-eslint');
5758

58-
return gulp.src('test/**/*.js')
59-
.pipe(eslint())
60-
.pipe(eslint.format());
59+
return gulp.src(task.src)
60+
.pipe(eslint())
61+
.pipe(eslint.format());
62+
});
6163
});
6264

6365

test/stage1/analytics.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
(function() {
22
'use strict';
3+
// PhantomJS s not supporting web components yet.
4+
// And some polyfills (as webcomponents/webcomponents.js) are not worked well.
5+
if (!('registerElement' in document)) { return; }
6+
37
var XFlyingSushiMonsterProto = Object.create(HTMLDivElement.prototype);
48
XFlyingSushiMonsterProto.createdCallback = function() {
59
this.textContent = '\uD83C\uDF63';

test/stage1/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
</script>
4242
<script>mocha.setup('bdd')</script>
4343
<script src="analytics.js"></script>
44+
<script>
45+
describe.skipWhenPhantomJS = window.mochaPhantomJS ? describe.skip : describe;
46+
</script>
4447
<script src="tests.js"></script>
4548
<script>
4649
if (window.mochaPhantomJS) { mochaPhantomJS.run(); }

test/stage1/tests.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
4+
35
describe('ステージ1(意図した DOM 要素を取得できるようになる)', function(){
46
describe('DOM インターフェース編', function() {
57
it('1 番の赤色の要素(ID は "firebrick")が1つ取得できる', function(){
@@ -39,8 +41,8 @@ describe('ステージ1(意図した DOM 要素を取得できるようにな
3941
// 参考資料
4042
// https://developer.mozilla.org/ja/docs/Web/API/Document/getElementsByClassName
4143

42-
expect(elements).to.be.instanceof(HTMLCollection);
43-
expect(elements).to.have.property('class', elementClassName);
44+
expect(elements).to.have.length(1);
45+
expect(elements[0]).to.have.property('className', elementClassName);
4446
});
4547

4648

@@ -53,7 +55,7 @@ describe('ステージ1(意図した DOM 要素を取得できるようにな
5355
// 参考資料
5456
// https://developer.mozilla.org/ja/docs/Web/API/Document/getElementsByClassName
5557

56-
expect(elements).to.be.instanceof(HTMLCollection);
58+
expect(elements).to.have.length(2);
5759
expect(elements[0]).to.have.property('className', elementClassName);
5860
expect(elements[1]).to.have.property('className', elementClassName);
5961
});
@@ -68,7 +70,7 @@ describe('ステージ1(意図した DOM 要素を取得できるようにな
6870
// 参考資料
6971
// https://developer.mozilla.org/ja/docs/Web/API/Document/getElementsByTagName
7072

71-
expect(elements).to.be.instanceof(HTMLCollection);
73+
expect(elements).to.have.length(1);
7274
expect(elements[0]).to.have.property('tagName', elementTagName.toUpperCase());
7375
});
7476

@@ -214,7 +216,7 @@ describe('ステージ1(意図した DOM 要素を取得できるようにな
214216

215217
// チュートリアル
216218
//
217-
// 'change me!' を document.querySelector('.js-training:nth-child(2) li');
219+
// 'change me!' を '.js-training:nth-child(2) li';
218220
// に書き換えてください。
219221
var selector = 'change me!';
220222

@@ -325,7 +327,7 @@ describe('ステージ1(意図した DOM 要素を取得できるようにな
325327
});
326328

327329

328-
describe('エクストラステージ', function(){
330+
describe.skipWhenPhantomJS('エクストラステージ', function(){
329331
it('動いている寿司要素を取得する', function(){
330332

331333
// 'change me!' を書き換えてください。

test/stage2/tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
34
describe('ステージ2(意図した通りに DOM 要素のスタイルを変更できるようになる)', function(){
45
describe('DOMインターフェース編', function() {
56
it('寿司が表示されている要素の寿司を2つに増やす', function(){

test/stage4/.eslintrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"mocha": true
5+
},
6+
"globals": {
7+
"$": false,
8+
"jQuery": false,
9+
"HTMLCollection": false
10+
}
11+
}

test/stage4/index.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>mixi JS Training</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link href="http://img.mixi.net/img/basic/favicon.ico" type="image/vnd.microsoft.icon" rel="icon">
8+
<link href="http://img.mixi.net/img/basic/favicon.ico" type="image/vnd.microsoft.icon" rel="shortcut icon">
9+
<link rel="stylesheet" href="/modules/mocha/mocha.css">
10+
<link rel="stylesheet" href="../common/mocha-patch.css">
11+
<link rel="stylesheet" href="style.css">
12+
</head>
13+
<body>
14+
<div class="js-training-container">
15+
<ul class="js-training">
16+
<li id="firebrick">1</li>
17+
<li id="chocolate">2</li>
18+
<li class="mediumseagreen">3</li>
19+
<li class="turquoise"><input type="number" value="0"></li>
20+
</ul>
21+
</div>
22+
<div id="mocha"></div>
23+
<script src="/modules/jquery/dist/jquery.js"></script>
24+
<script src="/modules/mocha/mocha.js"></script>
25+
<script src="/modules/chai/chai.js"></script>
26+
<script src="/modules/chai-jquery/chai-jquery.js"></script>
27+
<script>
28+
mocha.ui('bdd');
29+
mocha.reporter('html');
30+
expect = chai.expect;
31+
</script>
32+
<script>mocha.setup('bdd')</script>
33+
<script src="tests.js"></script>
34+
<script>
35+
if (window.mochaPhantomJS) { mochaPhantomJS.run(); }
36+
else { mocha.run(); }
37+
</script>
38+
</body>
39+
</html>

test/stage4/style.css

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
.js-training-container {
2+
position: relative;
3+
}
4+
5+
.js-training {
6+
position: relative;
7+
z-index:10;
8+
display: -webkit-flex;
9+
display: flex;
10+
list-style: none;
11+
margin: 10px;
12+
padding: 0;
13+
font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;
14+
}
15+
16+
.js-training li {
17+
-webkit-flex: 1;
18+
flex: 1;
19+
margin: 0;
20+
padding: 0;
21+
height: 40px;
22+
line-height: 40px;
23+
text-align: center;
24+
color: white;
25+
font-weight: bold;
26+
font-size: 130%;
27+
text-shadow: 0 1px 0 hsla(0, 0%, 0%, 0.3);
28+
}
29+
30+
.js-training #firebrick {
31+
background-color: firebrick;
32+
font-weight: normal;
33+
}
34+
35+
.js-training #chocolate {
36+
background-color: chocolate;
37+
font-weight: normal;
38+
}
39+
40+
.js-training .mediumseagreen {
41+
background-color: mediumseagreen;
42+
font-weight: normal;
43+
}
44+
45+
.js-training .turquoise {
46+
background-color: turquoise;
47+
font-weight: normal;
48+
}
49+
50+
.js-training blockquote {
51+
background-color: steelblue;
52+
margin: 0;
53+
height: 40px;
54+
font-size: 1;
55+
font-weight: normal;
56+
}
57+
58+
.js-training [data-js-training="blueviolet"] {
59+
background-color: blueviolet;
60+
}
61+
62+
.js-training #brown {
63+
background-color: brown;
64+
font-weight: normal;
65+
}
66+
67+
.js-training #darkorange {
68+
background-color: darkorange;
69+
font-weight: normal;
70+
}
71+
72+
.js-training .limegreen {
73+
background-color: limegreen;
74+
font-weight: normal;
75+
}
76+
77+
.js-training .mediumturquoise {
78+
background-color: mediumturquoise;
79+
font-weight: normal;
80+
}
81+
82+
.js-training p {
83+
margin: 0;
84+
padding: 0;
85+
background-color: cornflowerblue;
86+
font-weight: normal;
87+
}
88+
89+
.js-training [data-js-training="darkorchid"] {
90+
background-color: darkorchid;
91+
}
92+
93+
.js-training-trick {
94+
z-index: 1;
95+
position: absolute;
96+
top: 0;
97+
height: 40px;
98+
line-height: 40px;
99+
text-indent: 60%;
100+
}

test/stage4/tests.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
'use strict';
2+
3+
describe('ステージ4(意図した通りに DOM 要素の構造を変更できるようになる)', function() {
4+
describe('DOMインターフェース編', function() {
5+
it('1 番の要素の click イベントで要素内の数字を 1 ずつ大きくできる', function() {
6+
// チュートリアル
7+
//
8+
// 2 通りの代表的な書き方があります。
9+
//
10+
// jQuery じゃない版:
11+
//
12+
// var element = document.getElementById('firebrick');
13+
// element.addEventListener('click', function() {
14+
// element.textContent = Number(element.textContent) + 1;
15+
// });
16+
//
17+
// jQuery 版:
18+
//
19+
$('#firebrick').on('click', function(event) {
20+
var $target = $(event.target);
21+
$target.text(Number($target.text()) + 1);
22+
});
23+
//
24+
// ここに上記のどちらかのコードを記述してください。
25+
26+
27+
var firebrick = document.getElementById('firebrick');
28+
firebrick.dispatchEvent(createClickEvent());
29+
expect(firebrick).to.have.property('textContent', '2');
30+
31+
firebrick.dispatchEvent(createClickEvent());
32+
expect(firebrick).to.have.property('textContent', '3');
33+
});
34+
35+
36+
it('2 番の要素の click イベントで要素内の数字を 1 ずつ大きくできる', function() {
37+
38+
// ここにコードを記述してください。
39+
40+
$('#chocolate').on('click', function(event) {
41+
var $target = $(event.target);
42+
$target.text(Number($target.text()) - 1);
43+
});
44+
45+
var chocolate = document.getElementById('chocolate');
46+
chocolate.dispatchEvent(createClickEvent());
47+
expect(chocolate).to.have.property('textContent', '1');
48+
49+
chocolate.dispatchEvent(createClickEvent());
50+
expect(chocolate).to.have.property('textContent', '0');
51+
});
52+
53+
54+
it('3 番の要素の click イベントで要素を 10 度ずつ回転できる', function() {
55+
56+
// ここにコードを記述してください。
57+
58+
var deg = 0;
59+
$('.mediumseagreen').on('click', function(event) {
60+
deg += 10;
61+
$(event.target).css('transform', 'rotate(' + deg + 'deg)');
62+
});
63+
64+
var mediumseagreen = document.querySelector('.mediumseagreen');
65+
mediumseagreen.dispatchEvent(createClickEvent());
66+
expect(mediumseagreen).to.have.deep.property(
67+
secret('fglyr.genafsbez'), secret('ebgngr(10qrt)'));
68+
69+
mediumseagreen.dispatchEvent(createClickEvent());
70+
expect(mediumseagreen).to.have.deep.property(
71+
secret('fglyr.genafsbez'), secret('ebgngr(20qrt)'));
72+
});
73+
74+
75+
it('4 番の要素のに入力された角度に回転できる', function() {
76+
77+
// ここにコードを記述してください。
78+
79+
$('input').on('change', function() {
80+
var deg = $('input').val();
81+
$('.turquoise').css('transform', 'rotate(' + deg + 'deg)');
82+
});
83+
84+
var turquoise = document.querySelector('.turquoise');
85+
var turquoiseInput = turquoise.querySelector('input');
86+
87+
simulateChangeEvent(turquoiseInput, 10);
88+
expect(turquoise).to.have.deep.property(
89+
secret('fglyr.genafsbez'), secret('ebgngr(10qrt)'));
90+
91+
simulateChangeEvent(turquoiseInput, 20);
92+
expect(turquoise).to.have.deep.property(
93+
secret('fglyr.genafsbez'), secret('ebgngr(20qrt)'));
94+
});
95+
});
96+
});
97+
98+
99+
function createClickEvent() {
100+
var event = document.createEvent('MouseEvents');
101+
event.initMouseEvent('click', true, true, window,
102+
0, 0, 0, 80, 20, false, false, false, false, 0, null);
103+
return event;
104+
}
105+
106+
function simulateChangeEvent(inputElement, newValue) {
107+
inputElement.value = newValue;
108+
inputElement.dispatchEvent(createChangeEvent());
109+
}
110+
111+
function createChangeEvent() {
112+
var event = document.createEvent('HTMLEvents');
113+
event.initEvent('change', true, true);
114+
115+
return event;
116+
}
117+
118+
119+
function secret(str) {
120+
// Copyright (c) 2012 K.Adam White
121+
// Released under the MIT license
122+
// https://github.com/kadamwhite/jquery.rot13/blob/master/LICENSE-MIT
123+
return Array.prototype.map.call(str, function(char) {
124+
if (!char.match(/[A-Za-z]/)) { return char; }
125+
126+
var charCode = char.charCodeAt(0);
127+
if(charCode < 97) {
128+
charCode = (charCode - 52) % 26 + 65;
129+
} else {
130+
charCode = (charCode - 84) % 26 + 97;
131+
}
132+
return String.fromCharCode(charCode);
133+
}).join('');
134+
}

0 commit comments

Comments
 (0)