Skip to content

Commit f264c32

Browse files
author
Luke Dawkes
committed
Added $announcer.setComplementRoute
+ Added TypeScript definitions + Added Cypress tests ~ Modified example routes to use a beforeEnter handler so that tests can be run against the new method
1 parent cef4df0 commit f264c32

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

example/src/router.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ const router = new VueRouter({
1111
mode: 'history',
1212
routes: [
1313
{
14+
beforeEnter (to, from, next) {
15+
router.app.$announcer.setComplementRoute('has loaded')
16+
17+
next()
18+
},
1419
name: 'home',
1520
path: '/',
1621
component: Home,
@@ -19,6 +24,11 @@ const router = new VueRouter({
1924
}
2025
},
2126
{
27+
beforeEnter (to, from, next) {
28+
router.app.$announcer.setComplementRoute('has loaded')
29+
30+
next()
31+
},
2232
name: 'about',
2333
path: '/about',
2434
component: About,
@@ -27,6 +37,11 @@ const router = new VueRouter({
2737
}
2838
},
2939
{
40+
beforeEnter (to, from, next) {
41+
router.app.$announcer.setComplementRoute('has fully loaded')
42+
43+
next()
44+
},
3045
name: 'contact',
3146
path: '/contact',
3247
component: Contact,

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface Announcer
55
data: Record<string, any>;
66

77
set(message: string): void;
8+
9+
setComplementRoute(complementRoute: string): void;
810
}
911

1012
declare module 'vue/types/vue'

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ export default function install (Vue, options = {}, router = null) {
1515
})
1616
}
1717
},
18+
19+
setComplementRoute (complementRoute) {
20+
if (typeof (complementRoute) !== 'string') {
21+
return
22+
}
23+
24+
options.complementRoute = complementRoute
25+
},
1826
data: null
1927
}
2028

tests/e2e/integration/vue-announcer.test.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,31 @@ describe('Announcer test', () => {
1212
cy.get('[data-va="announcer"]').should('contain', 'has loaded')
1313
})
1414

15+
it('Should contain the custom complement when the route changes', () => {
16+
cy.get('a[href="/contact"]').click()
17+
cy.get('[data-va="announcer"]').should('contain', 'has fully loaded')
18+
})
19+
20+
it('Should handle setting the custom complement multiple times', () => {
21+
cy.get('a[href="/contact"]').click()
22+
cy.get('[data-va="announcer"]').should('contain', 'has fully loaded')
23+
24+
cy.get('a[href="/about"]').click()
25+
cy.get('[data-va="announcer"]').should('contain', 'has loaded')
26+
})
27+
1528
it('Should be equal toasted message with the announced', () => {
1629
cy.get('a[href="/about"]').click()
1730
cy.get('[data-va="toasted"]').click()
1831

1932
cy.get('.toasted-container')
20-
.find('.toasted')
21-
.invoke('text')
22-
.then(text1 => {
23-
cy.get('[data-va="announcer"]').invoke('text').should(text2 => {
24-
console.log(text1, text2)
25-
expect(text1).to.eq(text2)
33+
.find('.toasted')
34+
.invoke('text')
35+
.then(text1 => {
36+
cy.get('[data-va="announcer"]').invoke('text').should(text2 => {
37+
console.log(text1, text2)
38+
expect(text1).to.eq(text2)
39+
})
2640
})
27-
})
2841
})
2942
})

0 commit comments

Comments
 (0)