Create Grid component + Test. Index test adaptations

This commit is contained in:
2020-12-17 19:31:25 +01:00
parent baed9969b2
commit 10808b66b2
4 changed files with 53 additions and 5 deletions

19
components/Grid.vue Normal file
View File

@@ -0,0 +1,19 @@
<template>
<div class="grid">
{{ dataSource }}
</div>
</template>
<script>
export default {
name: "Grid",
props: {
dataSource: {
type: Array,
required: true
}
}
};
</script>
<style></style>

View File

@@ -3,19 +3,25 @@
<h2 class="text-3xl font-normal mx-auto block text-center mb-4"> <h2 class="text-3xl font-normal mx-auto block text-center mb-4">
Ghibli films Ghibli films
</h2> </h2>
<div class="films">{{ films }}</div> <Grid :dataSource="films" />
</section> </section>
</template> </template>
<script> <script>
export default { export default {
name: "Films", name: "Films",
components: { Grid: () => import("@/components/Grid") },
head: { head: {
titleTemplate: "%s - Films" titleTemplate: "%s - Films"
}, },
async asyncData({ $axios }) { async asyncData({ $axios }) {
const films = await $axios.$get("/api/films"); const films = await $axios.$get("/api/films");
return { films }; return { films };
},
data() {
return {
films: []
};
} }
}; };
</script> </script>

23
test/Grid.spec.js Normal file
View File

@@ -0,0 +1,23 @@
import { mount } from "@vue/test-utils";
import Grid from "@/components/Grid";
import mockFilms from "./fake-films.json";
describe("Grid", () => {
it("tests props", () => {
expect(Grid.props).toMatchObject({
dataSource: {
type: Array,
required: true
}
});
});
it("should render Grid instance", () => {
const wrapper = mount(Grid, {
propsData: {
dataSource: mockFilms
}
});
expect(JSON.parse(wrapper.text())).toEqual(mockFilms);
});
});

View File

@@ -1,4 +1,4 @@
import { mount } from "@vue/test-utils"; import { mount, shallowMount } from "@vue/test-utils";
import axios from "axios"; import axios from "axios";
import FilmsView from "@/pages/films"; import FilmsView from "@/pages/films";
import mockFilms from "./fake-films.json"; import mockFilms from "./fake-films.json";
@@ -14,7 +14,7 @@ describe("Films page", () => {
}); });
it("should get films from Ghibli API", async () => { it("should get films from Ghibli API", async () => {
let wrapper = mount(FilmsView, { let wrapper = shallowMount(FilmsView, {
mocks: { mocks: {
$axios: axios $axios: axios
} }
@@ -25,13 +25,13 @@ describe("Films page", () => {
); );
// Init page with mocked asyncData // Init page with mocked asyncData
wrapper = mount(FilmsView, { wrapper = shallowMount(FilmsView, {
mocks: { mocks: {
films: (await wrapper.vm.$options.asyncData(wrapper.vm)).films, films: (await wrapper.vm.$options.asyncData(wrapper.vm)).films,
$axios: axios $axios: axios
} }
}); });
expect(JSON.parse(wrapper.find(".films").text())).toEqual(mockFilms); expect(wrapper.findComponent({ name: "Grid" }).exists()).toBe(true);
}); });
}); });