|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { ref } from "vue"; |
| 3 | +import { flushPromises } from "@vue/test-utils"; |
| 4 | +import { mountSuspended } from "@nuxt/test-utils/runtime"; |
| 5 | +import { print } from "graphql"; |
| 6 | +import type { FieldNode, OperationDefinitionNode } from "graphql"; |
| 7 | +import MapPoolsPage from "~/pages/settings/application/map-pools/index.vue"; |
| 8 | + |
| 9 | +vi.mock("~/components/ui/sidebar/utils", async (original) => ({ |
| 10 | + ...(await original<Record<string, unknown>>()), |
| 11 | + useSidebar: () => ({ isMobile: ref(false) }), |
| 12 | +})); |
| 13 | + |
| 14 | +// A workshop map's name is its Workshop ID, so a mode row without the label |
| 15 | +// shows that number wherever the mode lists the map. |
| 16 | +const WORKSHOP_ID = "3199551320"; |
| 17 | + |
| 18 | +const row = (type: string, label: string | null, enabled = true) => ({ |
| 19 | + id: `aim_redline-${type}`, |
| 20 | + name: WORKSHOP_ID, |
| 21 | + label, |
| 22 | + type, |
| 23 | + poster: null, |
| 24 | + patch: null, |
| 25 | + active_pool: false, |
| 26 | + workshop_map_id: WORKSHOP_ID, |
| 27 | + enabled, |
| 28 | + deleted_at: null, |
| 29 | +}); |
| 30 | + |
| 31 | +const rootField = (query: any) => |
| 32 | + ( |
| 33 | + (query.definitions[0] as OperationDefinitionNode).selectionSet |
| 34 | + .selections[0] as FieldNode |
| 35 | + ).name.value; |
| 36 | + |
| 37 | +describe("map pools page, workshop map labels", () => { |
| 38 | + let push: Record<string, (data: unknown) => void> = {}; |
| 39 | + let mutations: string[] = []; |
| 40 | + let unmount: (() => void) | null = null; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + push = {}; |
| 44 | + mutations = []; |
| 45 | + const client = (useNuxtApp() as any).$apollo.defaultClient; |
| 46 | + |
| 47 | + vi.spyOn(client, "subscribe").mockImplementation((options: any) => ({ |
| 48 | + subscribe(observer: { next: (value: unknown) => void }) { |
| 49 | + push[rootField(options.query)] = (data) => observer.next({ data }); |
| 50 | + return { closed: false, unsubscribe() {} }; |
| 51 | + }, |
| 52 | + })); |
| 53 | + |
| 54 | + vi.spyOn(client, "watchQuery").mockImplementation((options: any) => ({ |
| 55 | + options, |
| 56 | + getCurrentResult: () => ({ loading: true, data: undefined }), |
| 57 | + subscribe: () => ({ closed: false, unsubscribe() {} }), |
| 58 | + refetch: () => new Promise(() => {}), |
| 59 | + setVariables: () => Promise.resolve(), |
| 60 | + setOptions: () => Promise.resolve(), |
| 61 | + stopPolling() {}, |
| 62 | + getLastError() {}, |
| 63 | + getLastResult() {}, |
| 64 | + resetLastResults() {}, |
| 65 | + })); |
| 66 | + |
| 67 | + vi.spyOn(client, "mutate").mockImplementation(async (options: any) => { |
| 68 | + mutations.push(print(options.mutation)); |
| 69 | + return { data: {} }; |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + afterEach(() => { |
| 74 | + unmount?.(); |
| 75 | + unmount = null; |
| 76 | + vi.restoreAllMocks(); |
| 77 | + }); |
| 78 | + |
| 79 | + async function mountWith(maps: Array<ReturnType<typeof row>>) { |
| 80 | + const wrapper = await mountSuspended(MapPoolsPage, { |
| 81 | + global: { mixins: (useNuxtApp().vueApp as any)._context.mixins }, |
| 82 | + }); |
| 83 | + unmount = () => wrapper.unmount(); |
| 84 | + await flushPromises(); |
| 85 | + push.maps({ maps }); |
| 86 | + push.map_pools({ map_pools: [] }); |
| 87 | + await flushPromises(); |
| 88 | + return wrapper; |
| 89 | + } |
| 90 | + |
| 91 | + async function toggleMode(wrapper: any, type: string) { |
| 92 | + const toggle = wrapper |
| 93 | + .findAll('tbody button[role="switch"]') |
| 94 | + .find( |
| 95 | + (button: any) => |
| 96 | + button.element.parentElement?.textContent?.trim() === type, |
| 97 | + ); |
| 98 | + expect(toggle, `${type} switch`).toBeDefined(); |
| 99 | + await toggle.trigger("click"); |
| 100 | + await flushPromises(); |
| 101 | + expect(mutations).toHaveLength(1); |
| 102 | + return mutations[0]; |
| 103 | + } |
| 104 | + |
| 105 | + const updateColumns = (mutation: string) => |
| 106 | + (mutation.match(/update_columns:\s*\[([^\]]*)\]/)?.[1] ?? "") |
| 107 | + .split(",") |
| 108 | + .map((column) => column.trim().replace(/"/g, "")); |
| 109 | + |
| 110 | + it("shows the label when the first row it gets for the map is a copy without one", async () => { |
| 111 | + const wrapper = await mountWith([ |
| 112 | + row("Duel", null), |
| 113 | + row("Competitive", "aim_redline"), |
| 114 | + ]); |
| 115 | + |
| 116 | + const display = wrapper.get("tbody tr td").text(); |
| 117 | + expect(display).toBe("aim_redline"); |
| 118 | + }); |
| 119 | + |
| 120 | + it("copies the label when the map is turned on for another mode", async () => { |
| 121 | + const wrapper = await mountWith([ |
| 122 | + row("Duel", null), |
| 123 | + row("Competitive", "aim_redline"), |
| 124 | + ]); |
| 125 | + |
| 126 | + const mutation = await toggleMode(wrapper, "Wingman"); |
| 127 | + |
| 128 | + expect(mutation).toContain("insert_maps"); |
| 129 | + expect(mutation).toMatch(/type:\s*Wingman/); |
| 130 | + expect(mutation).toMatch(/name:\s*"3199551320"/); |
| 131 | + expect(mutation).toMatch(/workshop_map_id:\s*"3199551320"/); |
| 132 | + expect(mutation).toMatch(/label:\s*"aim_redline"/); |
| 133 | + }); |
| 134 | + |
| 135 | + it("gives a mode's old copy the label when that mode is turned back on", async () => { |
| 136 | + const wrapper = await mountWith([ |
| 137 | + row("Competitive", "aim_redline"), |
| 138 | + row("Duel", null, false), |
| 139 | + ]); |
| 140 | + |
| 141 | + const mutation = await toggleMode(wrapper, "Duel"); |
| 142 | + |
| 143 | + expect(updateColumns(mutation).sort()).toEqual(["enabled", "label"]); |
| 144 | + }); |
| 145 | + |
| 146 | + it("never blanks a copy's label from a map that has none", async () => { |
| 147 | + const wrapper = await mountWith([row("Competitive", null)]); |
| 148 | + |
| 149 | + const mutation = await toggleMode(wrapper, "Wingman"); |
| 150 | + |
| 151 | + expect(updateColumns(mutation)).toEqual(["enabled"]); |
| 152 | + }); |
| 153 | +}); |
0 commit comments