diff --git a/src/components/ChatComponents/RentalMessage.vue b/src/components/ChatComponents/RentalMessage.vue index df2823fec366f458226cb8c7b5d0bfeb9e701260..c864d36bff56d845b8352e2664afcb4dd4f50ae6 100644 --- a/src/components/ChatComponents/RentalMessage.vue +++ b/src/components/ChatComponents/RentalMessage.vue @@ -100,12 +100,14 @@ export default { null, { headers: tokenHeader() } ); + this.$router.go(0); }, async reject() { await axios.delete( process.env.VUE_APP_BASEURL + `renting/${this.rent.rentId}/delete`, { headers: tokenHeader() } ); + this.$router.go(0); }, async getImage() { let images = await getItemPictures(this.rent.listingId); diff --git a/src/components/RentingComponents/ItemInfo.vue b/src/components/RentingComponents/ItemInfo.vue index beee721738d6feb6896c2c15cbb65ab6f9dda62f..bfe9c9af67080b66f1e26e13f7a021f4772b05c9 100644 --- a/src/components/RentingComponents/ItemInfo.vue +++ b/src/components/RentingComponents/ItemInfo.vue @@ -75,6 +75,7 @@ <p class="text-xl font-semibold text-gray-900"> Total pris: {{ totPrice }} kr </p> + <p v-if="error" style="color: red;">Dato er påkrevd</p> <button class="px-4 py-2 font-medium tracking-wide text-white capitalize transition-colors duration-200 transform bg-gray-500 rounded-md focus:outline-none focus:ring focus:ring-opacity-80" v-bind:class="{ colorChange: allowForRent }" @@ -107,6 +108,7 @@ export default { data() { return { confirm: false, + error: false, item: { listingID: 0, title: "", @@ -153,6 +155,8 @@ export default { if (this.allowForRent) { this.confirm = true; this.createPushItem(); + } else { + this.error = true; } }, createPushItem() { @@ -212,11 +216,12 @@ export default { this.rentingEndDate = dateOfsomthing.endDate; this.calculateTotPrice(); this.allowForRent = true; + this.error = false; } }, calculateTotPrice() { let amountOfDays = this.rentingEndDate - this.rentingStartDate; - amountOfDays = amountOfDays / 86400000; + amountOfDays = Math.ceil(amountOfDays / 86400000); this.totPrice = this.item.pricePerDay * amountOfDays; }, }, diff --git a/tests/unit/component-tests/ChatComponentsTest/RentalMessage.spec.js b/tests/unit/component-tests/ChatComponentsTest/RentalMessage.spec.js index c08130180924969b4b57b9c0a70eb2a50e3e35c2..5dd91c096296e40b474abc2d8e1fb61525eafc8d 100644 --- a/tests/unit/component-tests/ChatComponentsTest/RentalMessage.spec.js +++ b/tests/unit/component-tests/ChatComponentsTest/RentalMessage.spec.js @@ -27,6 +27,9 @@ jest.mock("axios"); describe("RentalMessage.vue", () => { let wrapper; + const mockRouter = { + go: jest.fn(), + } beforeEach(() => { wrapper = shallowMount(RentalMessage, { propsData: { @@ -47,6 +50,11 @@ describe("RentalMessage.vue", () => { deleted: false, }, }, + global: { + mocks: { + $router: mockRouter, + }, + } }); }); diff --git a/tests/unit/component-tests/renting-compnents-tests/item-info.spec.js b/tests/unit/component-tests/renting-compnents-tests/item-info.spec.js new file mode 100644 index 0000000000000000000000000000000000000000..e9874670c92f064b47df05e727f8e9dff03953e8 --- /dev/null +++ b/tests/unit/component-tests/renting-compnents-tests/item-info.spec.js @@ -0,0 +1,109 @@ +import { shallowMount } from "@vue/test-utils"; +import ItemInfo from "@/components/RentingComponents/ItemInfo.vue"; + +const mockMethod = jest.spyOn(ItemInfo.methods, 'sendToConfirm') +const mockCreatePush = jest.spyOn(ItemInfo.methods, 'createPushItem') + +jest.mock("@/utils/apiutil", () => { + return { + getItem: () => { + return new Promise((resolve) => { + resolve({ + listingID: 0, + title: "Title", + description: "Description", + pricePerDay: 100, + address: "ABC ROAD 3", + userID: 1, + categoryNames: [], + communityIDs: [], + }); + }); + }, + getItemPictures: () => { + return new Promise((resolve) => { + resolve([]); + }); + } + }; + }); + +describe("ItemInfo component", () => { + let wrapper; + const mockRouter = { + push: jest.fn(), + currentRoute: { + value: { + params: { + id: "1", + } + } + } + }; + const mockStore = { + commit: jest.fn(), + }; + + beforeEach(() => { + wrapper = shallowMount(ItemInfo, { + global: { + mocks: { + $router: mockRouter, + $store: mockStore, + }, + }, + }); + }); + + it("is instantiated", () => { + expect(wrapper.exists()).toBeTruthy(); + }) + + it("Check that title is displayed", () => { + expect(wrapper.findAll("h1")[0].text()).toBe("Title"); + }) + + it("Check that button cannot be clicked if date is not selected", async () => { + const jestCreatePushItemMock = jest.fn(); + wrapper.vm.createPushItem = jestCreatePushItemMock; + + // Click rent button + wrapper.find("button").trigger("click"); + + // wait a tick + await wrapper.vm.$nextTick(); + + // Check that jestMock was not clicked + expect(mockMethod).toHaveBeenCalledTimes(1); + expect(mockCreatePush).toHaveBeenCalledTimes(0); + // Check that the last p contains "Dato er påkrevd" + expect(wrapper.findAll("p")[wrapper.findAll("p").length - 1].text()).toBe("Dato er påkrevd"); + + }) + + it("Check that send to confirm works", async () => { + // Set valid days + wrapper.vm.setDate({ + startDate: "2020-01-01", + endDate: "2020-02-01", + }) + wrapper.find("button").trigger("click"); + // wait a tick + await wrapper.vm.$nextTick(); + + // Check that method to change component was called + expect(mockCreatePush).toHaveBeenCalledTimes(1); + }) + + it("Test that price calculation works", async () => { + wrapper.vm.setDate({ + startDate: new Date("2022-01-01"), + endDate: new Date("2022-01-03"), + }) + // wait a tick + await wrapper.vm.$nextTick(); + await wrapper.vm.$nextTick(); + + expect(wrapper.vm.totPrice).toBe(200); + }) +}); \ No newline at end of file