Skip to content
This repository was archived by the owner on Oct 13, 2022. It is now read-only.

vue2 mocked router read/write example #90

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions vue-cli-2/src/components/RouterExample.spec.ct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { mount } from "@cypress/vue";
import RouterExample from "./RouterExample.vue";

const mockRoute = {
name: "test",
query: {
myParam: "defaultValue",
},
};

const mockRouter = ({ pushSpy }) => ({
push: pushSpy,
});

const mountComponent = ({ pushSpy }) => {
return mount(RouterExample, {
mocks: {
$route: mockRoute,
$router: mockRouter({ pushSpy }),
},
});
};

describe("Date Range Selection Popup", () => {
let pushSpy;
beforeEach(() => {
pushSpy = cy.spy();
mountComponent({ pushSpy });
});

it("Reads default query param value", () => {
cy.get("div").contains("defaultValue").should("exist");
});
it("Writes new value to mocked router", () => {
cy.get("button")
.click()
.then(() => {
expect(pushSpy).to.be.calledWith({
name: 'test',
query: {
myParam: "newVal",
},
});
});
});
});
40 changes: 40 additions & 0 deletions vue-cli-2/src/components/RouterExample.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<template>
<div class="hello">

<h1>This component reads from and writes to a query param</h1>
<div>{{ myParam }}</div>

<button @click="updateQueryParams">updateQueryParams</button>
</div>
</template>

<script>
export default {
name: "RouterExample",
props: {
msg: String,
},
computed: {
// Current value of myParam
myParam() {
return this.$route.query.myParam;
},
},
methods: {
// Sets a new value
updateQueryParams() {
this.$router
.push({
name: this.$route.name,
query: {
myParam: 'newVal'
},
})
},
},
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>