Skip to content

Commit 2b16392

Browse files
author
Rija Menage
committed
Add new component to display the date for when page was loaded
the function used for returning current date must be in 'methods:' not 'computed:' because the later is cached on reactive dependencies so getting current time is not going to work. We need to mock the current time in the test. Jasmine's Clock class is made for that purpose. Just make sure Jasmine.clock() is called before the component is mounted otherwise the component will pick up the real Date class.
1 parent af0638a commit 2b16392

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/App.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
<main>
99
<UserNameComponent />
1010
</main>
11-
<footer></footer>
11+
<footer>
12+
<PageInfoComponent />
13+
</footer>
1214
</div>
1315
</template>
1416
<script>
1517
import UserNameComponent from './components/UserNameComponent.vue'
18+
import PageInfoComponent from './components/PageInfoComponent.vue'
1619
export default {
1720
data: function() {
1821
return {
@@ -23,7 +26,8 @@ export default {
2326
document.title = this.title
2427
},
2528
components: {
26-
UserNameComponent
29+
UserNameComponent,
30+
PageInfoComponent
2731
}
2832
}
2933
</script>

src/components/PageInfoComponent.vue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<template>
2+
<div class="footer">
3+
Page loaded on the wonderful day of {{ loadedDate() }}
4+
</div>
5+
</template>
6+
<style>
7+
</style>
8+
<script>
9+
export default {
10+
methods: {
11+
loadedDate: function () {
12+
return new Date().toDateString()
13+
}
14+
}
15+
}
16+
</script>

test/PageInfoComponent.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Vue from 'vue'
2+
import { shallowMount } from '@vue/test-utils'
3+
import PageInfoComponent from '../src/components/PageInfoComponent.vue'
4+
5+
const factory = function (values = {}) {
6+
return shallowMount(PageInfoComponent, {
7+
data () {
8+
return {
9+
...values
10+
}
11+
}
12+
})
13+
}
14+
15+
describe('Page Info Component', function () {
16+
it('should show the date the page was loaded', function () {
17+
18+
const theFakeNow = new Date('2016-05-03')
19+
jasmine.clock().mockDate(theFakeNow) //install the fake Date class and mock the current date
20+
const wrapper = factory() //make sure this come after Date's class has been overwritten
21+
expect(wrapper.find('.footer').text()).toContain(new Date().toDateString())
22+
})
23+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy