|
| 1 | +import { DOMWrapper, mount, VueWrapper } from "@vue/test-utils"; |
| 2 | +import { createVuetify } from "vuetify"; |
| 3 | +import MockAdapter from "axios-mock-adapter"; |
| 4 | +import { expect, describe, it, beforeEach, vi } from "vitest"; |
| 5 | +import { store, key } from "@/store"; |
| 6 | +import ContainerAdd from "@/components/Containers/ContainerAdd.vue"; |
| 7 | +import { router } from "@/router"; |
| 8 | +import { namespacesApi, billingApi, devicesApi } from "@/api/http"; |
| 9 | +import { SnackbarPlugin } from "@/plugins/snackbar"; |
| 10 | + |
| 11 | +const node = document.createElement("div"); |
| 12 | +node.setAttribute("id", "app"); |
| 13 | +document.body.appendChild(node); |
| 14 | + |
| 15 | +const devices = [ |
| 16 | + { |
| 17 | + uid: "a582b47a42d", |
| 18 | + name: "39-5e-2a", |
| 19 | + identity: { |
| 20 | + mac: "00:00:00:00:00:00", |
| 21 | + }, |
| 22 | + info: { |
| 23 | + id: "linuxmint", |
| 24 | + pretty_name: "Linux Mint 19.3", |
| 25 | + version: "", |
| 26 | + }, |
| 27 | + public_key: "----- PUBLIC KEY -----", |
| 28 | + tenant_id: "fake-tenant-data", |
| 29 | + last_seen: "2020-05-20T18:58:53.276Z", |
| 30 | + online: false, |
| 31 | + namespace: "user", |
| 32 | + status: "accepted", |
| 33 | + }, |
| 34 | + { |
| 35 | + uid: "a582b47a42e", |
| 36 | + name: "39-5e-2b", |
| 37 | + identity: { |
| 38 | + mac: "00:00:00:00:00:00", |
| 39 | + }, |
| 40 | + info: { |
| 41 | + id: "linuxmint", |
| 42 | + pretty_name: "Linux Mint 19.3", |
| 43 | + version: "", |
| 44 | + }, |
| 45 | + public_key: "----- PUBLIC KEY -----", |
| 46 | + tenant_id: "fake-tenant-data", |
| 47 | + last_seen: "2020-05-20T19:58:53.276Z", |
| 48 | + online: true, |
| 49 | + namespace: "user", |
| 50 | + status: "accepted", |
| 51 | + }, |
| 52 | +]; |
| 53 | + |
| 54 | +const members = [ |
| 55 | + { |
| 56 | + id: "xxxxxxxx", |
| 57 | + username: "test", |
| 58 | + role: "owner", |
| 59 | + }, |
| 60 | +]; |
| 61 | + |
| 62 | +const billingData = { |
| 63 | + active: false, |
| 64 | + status: "canceled", |
| 65 | + customer_id: "cus_test", |
| 66 | + subscription_id: "sub_test", |
| 67 | + current_period_end: 2068385820, |
| 68 | + created_at: "", |
| 69 | + updated_at: "", |
| 70 | + invoices: [], |
| 71 | +}; |
| 72 | + |
| 73 | +const namespaceData = { |
| 74 | + name: "test", |
| 75 | + owner: "xxxxxxxx", |
| 76 | + tenant_id: "fake-tenant-data", |
| 77 | + members, |
| 78 | + max_devices: 3, |
| 79 | + devices_count: 3, |
| 80 | + devices: 2, |
| 81 | + created_at: "", |
| 82 | + billing: billingData, |
| 83 | +}; |
| 84 | + |
| 85 | +const authData = { |
| 86 | + status: "", |
| 87 | + token: "", |
| 88 | + user: "test", |
| 89 | + name: "test", |
| 90 | + tenant: "fake-tenant-data", |
| 91 | + |
| 92 | + id: "xxxxxxxx", |
| 93 | + role: "owner", |
| 94 | +}; |
| 95 | + |
| 96 | +const customerData = { |
| 97 | + id: "cus_test", |
| 98 | + name: "test", |
| 99 | + |
| 100 | + payment_methods: [ |
| 101 | + { |
| 102 | + id: "test_id", |
| 103 | + number: "xxxxxxxxxxxx4242", |
| 104 | + brand: "visa", |
| 105 | + exp_month: 3, |
| 106 | + exp_year: 2029, |
| 107 | + cvc: "", |
| 108 | + default: true, |
| 109 | + }, |
| 110 | + ], |
| 111 | +}; |
| 112 | + |
| 113 | +const stats = { |
| 114 | + registered_devices: 3, |
| 115 | + online_devices: 1, |
| 116 | + active_sessions: 0, |
| 117 | + pending_devices: 0, |
| 118 | + rejected_devices: 0, |
| 119 | +}; |
| 120 | + |
| 121 | +describe("ContainerAdd", () => { |
| 122 | + let wrapper: VueWrapper<InstanceType<typeof ContainerAdd>>; |
| 123 | + |
| 124 | + const vuetify = createVuetify(); |
| 125 | + |
| 126 | + let mockNamespace: MockAdapter; |
| 127 | + let mockBilling: MockAdapter; |
| 128 | + let mockDevices: MockAdapter; |
| 129 | + |
| 130 | + beforeEach(async () => { |
| 131 | + const el = document.createElement("div"); |
| 132 | + document.body.appendChild(el); |
| 133 | + |
| 134 | + vi.useFakeTimers(); |
| 135 | + localStorage.setItem("tenant", "fake-tenant-data"); |
| 136 | + |
| 137 | + mockBilling = new MockAdapter(billingApi.getAxios()); |
| 138 | + mockNamespace = new MockAdapter(namespacesApi.getAxios()); |
| 139 | + mockDevices = new MockAdapter(devicesApi.getAxios()); |
| 140 | + |
| 141 | + mockNamespace.onGet("http://localhost:3000/api/namespaces/fake-tenant-data").reply(200, namespaceData); |
| 142 | + mockBilling.onGet("http://localhost:3000/api/billing/customer").reply(200, customerData); |
| 143 | + mockBilling.onGet("http://localhost:3000/api/billing/subscription").reply(200, billingData); |
| 144 | + mockBilling.onGet("http://localhost:3000/api/billing/devices-most-used").reply(200, devices); |
| 145 | + mockDevices.onGet("http://localhost:3000/api/devices?filter=&page=1&per_page=10&status=accepted").reply(200, devices); |
| 146 | + mockDevices.onGet("http://localhost:3000/api/stats").reply(200, stats); |
| 147 | + |
| 148 | + store.commit("auth/authSuccess", authData); |
| 149 | + store.commit("namespaces/setNamespace", namespaceData); |
| 150 | + store.commit("billing/setSubscription", billingData); |
| 151 | + store.commit("customer/setCustomer", customerData); |
| 152 | + store.commit("devices/setDeviceChooserStatus", true); |
| 153 | + |
| 154 | + wrapper = mount(ContainerAdd, { |
| 155 | + global: { |
| 156 | + plugins: [[store, key], vuetify, router, SnackbarPlugin], |
| 157 | + }, |
| 158 | + attachTo: el, |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + it("Is a Vue instance", () => { |
| 163 | + expect(wrapper.vm).toBeTruthy(); |
| 164 | + }); |
| 165 | + |
| 166 | + it("Renders the component", () => { |
| 167 | + expect(wrapper.html()).toMatchSnapshot(); |
| 168 | + }); |
| 169 | + |
| 170 | + it("Data is defined", () => { |
| 171 | + expect(wrapper.vm.$data).toBeDefined(); |
| 172 | + }); |
| 173 | + |
| 174 | + it("Renders the component data table", async () => { |
| 175 | + expect(wrapper.find('[data-test="device-add-btn"]').exists()).toBe(true); |
| 176 | + await wrapper.findComponent('[data-test="device-add-btn"]').trigger("click"); |
| 177 | + const dialog = new DOMWrapper(document.body); |
| 178 | + expect(dialog.find('[data-test="dialog"]').exists()).toBe(true); |
| 179 | + expect(dialog.find('[data-test="dialog-title"]').exists()).toBe(true); |
| 180 | + expect(dialog.find('[data-test="dialog-text"]').exists()).toBe(true); |
| 181 | + expect(dialog.find('[data-test="command-field"]').exists()).toBe(true); |
| 182 | + expect(dialog.find('[data-test="documentation-link"]').exists()).toBe(true); |
| 183 | + expect(dialog.find('[data-test="close-btn"]').exists()).toBe(true); |
| 184 | + }); |
| 185 | + |
| 186 | + it("Opens the dialog when add device button is clicked", async () => { |
| 187 | + const dialog = new DOMWrapper(document.body); |
| 188 | + expect(dialog.find('[data-test="dialog"]').exists()).toBe(true); |
| 189 | + |
| 190 | + await wrapper.find('[data-test="device-add-btn"]').trigger("click"); |
| 191 | + |
| 192 | + expect(dialog.find('[data-test="dialog"]').exists()).toBe(true); |
| 193 | + }); |
| 194 | +}); |
0 commit comments