-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
1246 lines (1214 loc) · 34.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Vue.js Training</title>
<link rel="icon" type="image/png" sizes="16x16" href="favicon.png">
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/pikestreet.css">
<link rel="stylesheet" href="css/github.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<section data-background-image="img/vue-bg.png">
<h2>Vue.js</h2>
<h3><i>/vjuː/</i></h3>
</section>
</section>
<section>
<section>
<h2>Vue.js</h2>
<ul>
<li>Framework(/Library) to create interfaces (views)</li>
<li>Oriented components</li>
<li>Easy to use, straightforward syntax</li>
<li>Use declarative rendering</li>
</ul>
<aside class="notes">
NPM Trends: http://www.npmtrends.com/angular-vs-react-vs-vue-vs-@angular/core
</aside>
</section>
<section>
<h2>Who's using VueJS</h2>
<ul>
<li>Gitlab</li>
<li>Laravel website</li>
<li>Baidu</li>
<li>Alibaba</li>
<li>Tencent</li>
<li>Xiaomi</li>
<li>Expedia</li>
<li><a href="https://newsfeed.fb.com">...and Facebook!</a></li>
</ul>
</section>
<section>
<h2>(quick) Setup</h2>
<pre class="fragment"><code data-trim>
<script src="https://unpkg.com/vue"></script>
</code></pre>
<pre class="fragment"><code data-trim>
<div id="app">
{{ message }}
</div>
</code></pre>
<pre class="fragment"><code data-trim>
var app = new Vue({
el: '#app',
data: {
message: 'Hello hostel!'
}
})
</code></pre>
</section>
<section>
<h2>Declarative</h2>
<h4>Tell <b>WHAT</b> you render instead of <b>HOW</b></h4>
<img src="./img/complex-simple.gif" alt="simple">
</section>
<section>
<h2>Declarative rendering</h2>
<img src="./img/reactive-rendering.png" alt="declarative rendering">
</section>
<section>
<h2>DevTools</h2>
<img src="./img/devtool.png" alt="declarative rendering" style="width: 60%;">
</section>
<section data-transition="none">
<h2>Virtual DOM</h2>
<img src="./img/virtual-dom-1.png" alt="virtual DOM" style="width: 80%;">
</section>
<section data-transition="none">
<h2>Virtual DOM</h2>
<img src="./img/virtual-dom-2.png" alt="virtual DOM" style="width: 80%;">
</section>
<section data-transition="none">
<h2>Virtual DOM</h2>
<img src="./img/virtual-dom-3.png" alt="virtual DOM" style="width: 80%;">
</section>
<section data-transition="none">
<h2>Virtual DOM</h2>
<img src="./img/virtual-dom-4.png" alt="virtual DOM" style="width: 80%;">
</section>
<section data-transition="none">
<h2>Virtual DOM</h2>
<img src="./img/virtual-dom-5.png" alt="virtual DOM" style="width: 80%;">
</section>
<section id="virtualdom">
<h2>Virtual DOM: Example</h2>
<div id="counterDemo">
<p>Counter: <span>{{ counter }}</span></p>
<p><i>(open Chrome DevTools)</i></p>
</div>
<script>
window.onload = () => {
Reveal.addEventListener('slidechanged', (event) => {
if (event.currentSlide.id === 'virtualdom') {
counterDemo.counter = 0
}
})
}
var counterDemo = new Vue({
el: '#counterDemo',
name: 'counter',
data: {
counter: 0
},
created () {
setInterval(() => {
this.counter++
}, 1000)
}
})
</script>
</section>
</section>
<section>
<section>
<h2>The Vue Syntax</h2>
</section>
<section>
<h2>Conditional rendering: v-if</h2>
<pre class="fragment"><code data-trim>
<div id="app">
<div>
<h2>{{ hostel.name }}</h2>
<a v-if="hostel.availability">Book now</a>
</div>
</div>
</code></pre>
<pre class="fragment"><code data-trim>
var app = new Vue({
el: '#app',
data: {
hostel: {
name: 'Happy Hostel',
availability: true
}
}
})
</code></pre>
</section>
<section>
<h2>Conditional rendering: v-else</h2>
<pre><code data-trim>
<div id="app">
<div>
<h2>{{ hostel.name }}</h2>
<a v-if="hostel.availability">Book now</a>
<a v-else disabled>Too late!</a>
</div>
</div>
</code></pre>
<pre><code data-trim>
var app = new Vue({
el: '#app',
data: {
hostel: {
name: 'Happy Hostel',
availability: true
}
}
})
</code></pre>
</section>
<section>
<h2>Conditional rendering: v-show</h2>
<pre class="fragment"><code data-trim>
<div id="app">
<div v-show="hostel.isActive">
<h2>{{ hostel.name }}</h2>
<a v-if="hostel.availability">Book now</a>
</div>
</div>
</code></pre>
<pre class="fragment"><code data-trim>
var app = new Vue({
el: '#app',
data: {
hostel: {
name: 'Happy Hostel',
availability: true,
isActive: true
}
}
})
</code></pre>
</section>
<section>
<h2>Loops: v-for</h2>
<pre class="fragment"><code data-trim>
<div id="app">
<div v-for="hostel in hostels">
<h2>{{ hostel.name }}</h2>
</div>
</div>
</code></pre>
<pre class="fragment"><code data-trim>
var app = new Vue({
el: '#app',
data: {
hostels: [
{ name: 'Happy Hostel' },
{ name: 'Awesome Hostel' },
{ name: 'Another Hostel' }
]
}
})
</code></pre>
</section>
<section>
<h2>Form Input Bindings: v-model</h2>
<pre class="fragment" data-fragment-index="0"><code data-trim>
<input v-model="message" placeholder="edit me">
<p>Message: {{ message }}</p>
</code></pre>
<h4 class="fragment" data-fragment-index="1">Example:</h4>
<div id="vue-model" class="fragment" data-fragment-index="1">
<input v-model="message" placeholder="edit me">
<p>Message: <span style="color: #42b983;">{{ message }}</span></p>
</div>
<script>
var vueModel = new Vue({
el: '#vue-model',
name: 'v-model',
data: {
message: 'Long live v-model!'
}
})
</script>
</section>
<section>
<h2>Computed properties</h2>
<pre class="fragment"><code data-trim>
const vueModel = new Vue({
el: '#app',
data: {
message: 'Long live v-model!'
},
computed: {
messageUpperCase () {
return this.message.toUpperCase()
}
}
})
</code></pre>
</section>
<section>
<h2>Workshop</h2>
<a href="http://slides.maxpou.fr/vuejs-training/_book/docs/part-1.html"><i class="fa fa-globe" aria-hidden="true"></i> Part 1: Playing with Vue Syntax</a>
</section>
</section>
<section>
<section>
<h2>Wait... I've to put everything on the same vue?</h2>
<h3 class="fragment">Nope.</h3>
</section>
</section>
<section>
<section>
<h2>A world of components</h2>
<img src="https://media1.giphy.com/media/l0JMrPWRQkTeg3jjO/giphy.gif">
</section>
<section>
<h2>What is a component?</h2>
<img src="./img/components.png" alt="components">
</section>
<section>
<h2>Communication between components</h2>
<img src="./img/communication.png">
</section>
<section>
<h2>our first component</h2>
<pre class="fragment"><code class="language-js" data-trim>
Vue.component('hostel-detail', {
template: `<div>I'm a hostel!</div>`
})
new Vue({
el: '#app'
})
</code></pre>
<pre class="fragment"><code data-trim>
<div id="app">
<hostel-detail></hostel-detail>
</div>
</code></pre>
</section>
<section>
<h2>Passing properties</h2>
<pre class="fragment"><code data-trim>
<div id="app">
<hostel-detail v-bind:name="'Cool hostel'"></hostel-detail>
</div>
</code></pre>
<pre class="fragment"><code class="language-js" data-trim>
Vue.component('hostel-detail', {
template: `<div>hostel: {{ name }}</div>`,
props: {
name: String
}
})
</code></pre>
<p class="fragment">shortcut: <code>:name="'Cool hostel'"</code></p>
</section>
<section>
<h2>Props validation</h2>
<pre><code class="language-js" data-trim>
Vue.component('hostel-price', {
template: `<div>Price: {{ price }}</div>`,
props: {
price: {
type: Number,
required: true,
validator (value) {
return value > 0
},
default: 100
}
}
})
</code></pre>
</section>
<section>
<h2>Emit events</h2>
<pre class="fragment"><code data-trim>
<div id="emitEvents">
<button v-on:click="addToCart(hostel)">{{ hostel.name }}</button>
</div>
</code></pre>
<pre class="fragment"><code data-trim>
new Vue({
el: '#emitEvents',
data: {
hostel: {
name: 'Cool hostel'
}
},
methods: {
addToCart (hostel) {
this.$emit('selecthostel', hostel)
}
}
})
</code></pre>
<div id="demo-event" class="fragment">
<h4>Example</h4>
<button v-on:click="addToCart(hostel)">{{ hostel.name }}</button>
</div>
<script>
var addToCart = new Vue({
el: '#demo-event',
name: 'addToCart',
data: {
hostel: {
name: 'Cool hostel'
}
},
methods: {
addToCart (hostel) {
this.$emit('selecthostel', hostel)
}
}
})
</script>
</section>
<section>
<h2>Events</h2>
<img src="./img/events.png" style="width: 80%;">
</section>
<section>
<h2>Listening events</h2>
<pre class="fragment"><code data-trim>
<div id="hostel-list">
<hostel-list-item v-for="(hostel, index) in properties" :key="index"
:hostel="hostel" v-on:select="add"></hostel-list-item>
<p>Cart: {{ cartToString }}</p>
</div>
</code></pre>
<pre class="fragment"><code data-trim>
Vue.component('hostel-list-item', {
template: '<button v-on:click="click">+ add {{ hostel }} to cart</button>',
props: ['hostel'],
methods: {
click () {
this.$emit('select', this.hostel)
}
}
})
var hostelList = new Vue({
el: '#hostel-list-demo',
name: 'hostelList',
data: {
properties: ['hostelA', 'hostelB'],
cart: []
},
computed: {
cartToString () {
return this.cart.length ? this.cart.join(' ') : '(empty)'
}
},
methods: {
add (hostel) {
this.cart.push(hostel)
}
}
})
</code></pre>
</section>
<section>
<h2>Listening events - Example</h2>
<div id="hostel-list-demo">
<hostel-list-item v-for="(hostel, index) in properties" :key="index"
:hostel="hostel" v-on:select="add"></hostel-list-item>
<p>Cart: {{ cartToString }}</p>
</div>
<script>
Vue.component('hostel-list-item', {
template: '<button v-on:click="click">+ add {{ hostel }} to cart</button>',
props: ['hostel'],
methods: {
click () {
this.$emit('select', this.hostel)
}
}
})
var hostelList = new Vue({
el: '#hostel-list-demo',
name: 'hostelList',
data: {
properties: ['hostelA', 'hostelB'],
cart: []
},
computed: {
cartToString () {
return this.cart.length ? this.cart.join(' ') : '(empty)'
}
},
methods: {
add (hostel) {
this.cart.push(hostel)
}
}
})
</script>
</section>
<section>
<h2>Watchers</h2>
<pre class="fragment" data-fragment-index="0"><code data-trim class="language-html">
<input v-model="appreciation" placeholder="say something good!">
<p>Vue.JS is...<span>{{ appreciation }}</span></p>
</code></pre>
<pre class="fragment" data-fragment-index="0"><code data-trim>
var vueWatcher = new Vue({
el: '#vue-watcher',
data: {
appreciation: ''
},
watch: {
appreciation (newappreciation) {
if (newappreciation === 'bad') {
this.appreciation = 'awesome'
}
}
}
})
</code></pre>
<div id="vue-watcher-demo" class="fragment" data-fragment-index="1">
<h4>Example: <input v-model="appreciation" placeholder="say something good!"></h4>
<p>Vue.JS is...<span style="color: #42b983;">{{ appreciation }}</span></p>
</div>
<script>
var vueWatcher = new Vue({
el: '#vue-watcher-demo',
name: 'watcher',
data: {
appreciation: ''
},
watch: {
appreciation (newappreciation) {
if (newappreciation === 'bad') {
this.appreciation = 'awesome'
}
}
}
})
</script>
</section>
<section>
<h2>Lifecycle</h2>
<img src="./img/lifecycle.png" style="width: 20%;">
</section>
<section>
<h2>Example with <code>mounted()</code></h2>
<pre class="fragment"><code class="language-html" data-trim>
<div id="lifecycle" v-if="starship">
<h2>{{ starship.name }}</h2>
<h3>{{ starship.model }}</h3>
</div>
</code></pre>
<pre class="fragment"><code class="language-js" data-trim>
const lifecycle = new Vue({
el: '#lifecycle',
data: {
starship: {}
},
mounted () {
fetch('https://swapi.co/api/starships/12/')
.then(response => response.json())
.then(data => {
this.starship = data
})
}
})
</code></pre>
</section>
<section>
<h2>Workshop</h2>
<a href="http://slides.maxpou.fr/vuejs-training/_book/docs/part-2.html"><i class="fa fa-globe" aria-hidden="true"></i> Part 2: Components</a>
</section>
</section>
<section>
<section>
<h2>The .vue File</h2>
</section>
<section>
<img src="./img/vue-file.png" alt="vue file" style="width: 50%;">
</section>
<section>
<h2>What's the magic behind</h2>
<img src="./img/webpack.png" alt="vue file" style="width: 50%;">
</section>
<section>
<h2>Benefit: Hot reload ❤️</h2>
<img src="./img/vue-hot.gif">
<p><i>hot reload > live reload</i></p>
</section>
<section>
<h2>Separation of concerns</h2>
<img src="./img/separation-concerns-1.png" alt="Separation of concerns" class="fragment" style="width: 45%;">
<img src="./img/separation-concerns-2.png" alt="Separation of concerns" class="fragment" style="width: 40%;">
</section>
<section>
<h2>Getting started!</h2>
<pre><code data-trim>
npm install -g vue-cli
vue init webpack my-project
cd my-project
npm install
npm run dev
</code></pre>
</section>
<section>
<h2>Workshop</h2>
<a href="http://slides.maxpou.fr/vuejs-training/_book/docs/part-3.html"><i class="fa fa-globe" aria-hidden="true"></i> Part 3: .vue files</a>
</section>
</section>
<section>
<section>
<h2>Vue ecosystem: HTTP</h2>
</section>
<section>
<h2>Let's find something cool</h2>
<img src="./img/google-ajax.png">
<h2 class="fragment" style="color:orange">Please DON'T! <br> ...and forget your jQuery habits!</h2>
</section>
<section>
<h2>Think JavaScript!</h2>
<img src="./img/google-ajax-2.png">
</section>
<section>
<h2>Vue ecosystem: HTTP</h2>
<h3><a href="https://github.com/axios/axios"><i class="fa fa-github" aria-hidden="true"></i> axios/axios</a></h3>
</section>
<section>
<h2>File architecture</h2>
<pre><code data-trim>
├── src/
│ ├── main.js
│ ├── App.vue
│ └── api/
│ ├── bookings.js
│ ├── hostel.js
│ └── user.js
│ └── components/
│ └── ...
│ └── .../
│ └── ...
</code></pre>
</section>
<section>
<h2>Example (1/2)</h2>
<pre><code data-trim>
// src/api/hostel.js
import axios from 'axios'
export function findAll (limit = 25) {
return axios.get('hostels/?limit=' + limit)
.then(response => response.data)
}
</code></pre>
</section>
<section>
<h2>Example (2/2)</h2>
<pre><code data-trim class="language-js">
import * as hostelApi from '../api/hostels'
export default {
data () {
return {
hostels: []
}
},
created () {
hostelApi.findAll().then((data) => {
this.hostels = data
})
}
}
</code></pre>
</section>
<section>
<h2>Workshop</h2>
<a href="http://slides.maxpou.fr/vuejs-training/_book/docs/part-4.html"><i class="fa fa-globe" aria-hidden="true"></i> Part 4: HTTP&Axios</a>
</section>
</section>
<section>
<section>
<h2>Vue ecosystem: Routing</h2>
<h3><a href="https://router.vuejs.org/en/"><i class="fa fa-globe" aria-hidden="true"></i> vue-router</a></h3>
</section>
<section>
<h2>Single Page Application</h2>
<img src="./img/spa.png">
</section>
<section>
<h2>Vue-Router</h2>
<ul>
<li>Simple to use</li>
<li>Allow nested routes</li>
<li>Window.history</li>
<li>Support SSR - Server Side Rendering</li>
<li>...</li>
</ul>
</section>
<section>
<h2>Installation</h2>
</section>
<section>
<h2>Example: Main template</h2>
<pre><code data-trim class="language-html">
<div id="app">
<p>
<router-link to="/account">Go to my account</router-link>
<router-link to="/bookings">Go to booking</router-link>
</p>
<router-view></router-view>
</div>
</code></pre>
</section>
<section>
<h2>Example: JS</h2>
<pre><code data-trim class="language-js">
const Account = { template: '<div>account</div>' }
const Bookings = { template: '<div>bookings</div>' }
const routes = [
{ path: '/account', component: Account },
{ path: '/bookings', component: Bookings }
]
const router = new VueRouter({
routes: routes
})
const app = new Vue({
router
}).$mount('#app')
</code></pre>
</section>
<section>
<h2>Workshop</h2>
<a href="http://slides.maxpou.fr/vuejs-training/_book/docs/part-5.html"><i class="fa fa-globe" aria-hidden="true"></i> Part 5: Routing</a>
</section>
</section>
<section>
<section>
<h2>Vue ecosystem: State Management</h2>
<h3><a href="https://router.vuejs.org/en/"><i class="fa fa-globe" aria-hidden="true"></i> vuex</a></h3>
</section>
<section>
<h2>Why you need a state managemet library?</h2>
<img src="./img/state-management.png">
<p class="fragment">Components need to share the same state</p>
</section>
<section>
<h2>What is Vuex?</h2>
<ul>
<li>Inspired by Flux, Redux and The Elm Architecture</li>
<li>Provide a centralized store</li>
<li>Easy to test!</li>
</ul>
<img src="./img/vuex-flow.png" style="width: 40%;margin-top: 15px;">
</section>
<section>
<h2>Idea behing Vuex</h2>
<img src="./img/vuex.png">
</section>
<section>
<h2>Core concepts</h2>
<ul>
<li class="fragment space">
<b>State</b>: An object containing the data<br>
<i>Ex: a list of hostels</i>
</li>
<li class="fragment space">
<b>Getters</b>: A state accessor<br>
<i>Ex: getHostels(), getHostelsFromBerlin()...</i>
</li>
<li class="fragment space">
<b>Mutations</b>: perform modifications of data in the state. Must be syncrhonous!<br>
<i>Ex: addRoomToSelection()</i>
</li>
<li class="fragment space">
<b>Actions</b>: similar to mutations. Instead of mutating the state, actions commit mutations. Can also be asynchronous<br>
<i>Ex: roomApi.fetch(), checkout()</i>
</li>
</ul>
</section>
<section>
<h2>Modules</h2>
</section>
<section>
<h2>File architecture</h2>
<pre><code data-trim>
├── src/
│ ├── main.js
│ ├── App.vue
│ └── api/
│ └── ...
│ └── components/
│ └── ...
│ └── store/
│ └── modules/
│ └── ...
│ ├── index.js
│ └── mutation-types.js
</code></pre>
</section>
<section>
<h2>Example - store side</h2>
<pre><code data-trim class="language-js">
// store/modules/hostel.js
import * as api from '../api'
import * as types from '../mutation-types'
export default {
state: {
hostels: []
},
getters = {
allHostels: state => state.hostels,
highRatedHostels: state => state.hostels.filter(h => h.score >= 7)
},
mutations: {
[types.RECEIVE_HOSTEL] (state, hostels) {
state.hostels = hostels
}
},
actions: {
loadCountryHostels ({ commit }, country) {
commit(types.LOAD_HOSTELS_BY_COUNTRY, { country })
api.getHostels(country).then(data => {
commit(types.RECEIVE_HOSTELS, { data })
})
}
}
}
</code></pre>
</section>
<section>
<h2>Example - Component side</h2>
<pre><code data-trim class="language-js">
// MyComponent.vue
import { mapGetters, mapActions } from 'vuex'
export default {
computed: mapGetters({
hostels: 'allHostels'
}),
methods: mapActions([
'loadCountryHostels'
]),
created () {
this.$store.dispatch('loadCountryHostels', 'Berlin')
}
}
</code></pre>
</section>
<section>
<h2>Why actions commit mutations?</h2>
<img src="./img/vuex-example-flow.png">
</section>
<section>
<h2>Why actions commit mutations?</h2>
<pre><code data-trim class="language-js">
// store/actions.js
export const addToSelection = ({ commit }, hostel) => {
commit(types.ADD_TO_SELECTION, { hostel })
}
</code></pre>
<pre><code data-trim class="language-js">
// store/modules/hostelList.js
[types.ADD_TO_SELECTION] (state, { hostel }) {
state.hostelList.slice(state.hostelList.indexOf(hostel), 1)
}
</code></pre>
<pre><code data-trim class="language-js">
// store/modules/hostelSelection.js
[types.ADD_TO_SELECTION] (state, { hostel }) {
state.selection.push(hostel)
}
</code></pre>
</section>
<section>
<h2>👍 Browsers devtools</h2>
<img src="./img/devtools-vuex.gif" style="width: 60%;">
<ul>
<li>Import/Export state</li>
<li>Time travel</li>
</ul>
</section>
<section>
<h2>Workshop</h2>
<a href="http://slides.maxpou.fr/vuejs-training/_book/docs/part-6.html"><i class="fa fa-globe" aria-hidden="true"></i> Part 6: State management with Vuex</a>
</section>
</section>
<section>
<section>
<h2>Vue ecosystem: Unit Testing</h2>
<h3><a href="https://github.com/facebook/jest"><i class="fa fa-globe" aria-hidden="true"></i> Jest</a></h3>
<h3><a href="https://vue-test-utils.vuejs.org/"><i class="fa fa-globe" aria-hidden="true"></i> vue/vue-test-utils</a></h3>
<img src="./img/jest.png" style="width: 50%;">
</section>
<section>
<h2>Why Jest?</h2>
<ul>
<li>Watch mode</li>
<li>Snapshot testing</li>
<li><s>Good</s> code coverage 😥</li>
</ul>
</section>
<section>
<h2>Watch mode</h2>
<img src="./img/jest-watch.gif">
</section>
<section>
<h2>Run with pattern</h2>
<img src="./img/jest-watch-filter.gif">
</section>
<section>
<h2>Installation</h2>
</section>
<section>
<h2>File Architecture</h2>
<pre><code data-trim>
├── test/
│ ├── __mocks__
│ ├── __snapshots__
│ └── api/
│ └── hostelApi.spec.js
│ └── components/
│ └── __snapshots__/
│ └── Selection.spec.js.snap
│ └── MyComponent.spec.js
│ └── store/
│ └── modules/
│ └── myModule.spec.js
</code></pre>
</section>
<section>
<h2>Assertions List</h2>
<pre><code data-trim>
expect(foo).toBe() // use ===
expect(foo).toEqual() // object have same values (not necessary same reference)
expect(true).toBeTruthy()
expect(false).toBeFalsy()
expect('banana').toContain('ban')
</code></pre>
<p><a href="https://facebook.github.io/jest/docs/en/expect.html"><i class="fa fa-globe" aria-hidden="true"></i> list of assertions</a></p>
</section>
<section>
<h2>Our first test</h2>
<iframe src="https://repl.it/languages/jest?lite=true" style="width: 100%;height: -webkit-fill-available;"></iframe>
</section>
<section>
<h3>Now, let's meet the real world!</h3>
</section>
<section>
<h2>Snapshot testing - example</h2>
<pre><code data-trim>
import { mount, shallow } from 'vue-test-utils'
import MyComponent from '@/components/MyComponent.vue'
import store from '@/store'
describe('Shop.vue', () => {
it('test initial rendering', () => {
const wrapper = mount(MyComponent, { store })
const template = wrapper.html()
expect(template).toMatchSnapshot()
})
})
</code></pre>
</section>
<section data-transition="none">
<h2>Snapshot testing - flow</h2>
<img src="./img/jest-snapshot-flow-1.png">
</section>
<section data-transition="none">
<h2>Snapshot testing - flow</h2>
<img src="./img/jest-snapshot-flow-2.png">
</section>
<section data-transition="none">
<h2>Snapshot testing - flow</h2>
<img src="./img/jest-snapshot-flow-3.png">
</section>
<section>
<h2>Testing component interactions (1/2)</h2>
<pre><code data-trim class="language-js" >
it('should do something on close', () => {
const wrapper = mount(MyComponent, { store })
const button = wrapper.findAll('div.btn')
button.trigger('click')
expect(/** ... */).toBe('true')
});
</code></pre>
</section>
<section>
<h2>Testing component interactions 🚧 TODO 🚧</h2>
<pre><code data-trim class="language-js" >
it('should do something on close', () => {
// 🚧 TODO 🚧
});
</code></pre>
</section>
<section>
<h2>Testing Axios calls - Mock part</h2>
<pre><code data-trim>
// tests/unit/specs/__mocks__/axios.js
import userBookingResponse from '../api/userBooking.response.json'
class Axios {
get (url) {
if (url === '/users/1/bookings'){
const data = userBookingResponse
}
const response = { status: 200, statusText: 'OK', data: data }
return Promise.resolve(response)
}
}
export default new Axios()
</code></pre>
</section>
<section>
<h2>Testing Axios calls - mock using dynamic imports</h2>
<pre><code class="language-js" data-trim data-noescape>
// tests/unit/specs/__mocks__/axios.js
class Axios {
get (fullUrl) {
const url = fullUrl.replace(process.env.API_URL, '')
switch (url) {
case 'users/1': return Promise.resolve({ data: <mark>import('../api/user.1.response.json')</mark> })
case 'hostels/?page=3': return Promise.resolve({ data: <mark>import('../api/hostels.3.response.json')</mark> })