|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +import { flushPromises, mount } from '@vue/test-utils' |
| 18 | +import { nextTick } from 'vue' |
| 19 | +import Query from '@/views/dataset/Query.vue' |
| 20 | +import { vi } from 'vitest' |
| 21 | + |
| 22 | +const FAKE_FUSEKI_URL = 'https://localhost:1234/fuseki/' |
| 23 | + |
| 24 | +const $routeMock = { |
| 25 | + query: {} |
| 26 | +} |
| 27 | + |
| 28 | +const mountFunction = options => { |
| 29 | + const mountOptions = Object.assign(options || {}, { |
| 30 | + shallow: true, |
| 31 | + global: { |
| 32 | + mocks: { |
| 33 | + $route: $routeMock, |
| 34 | + $fusekiService: { |
| 35 | + getFusekiUrl () { |
| 36 | + return FAKE_FUSEKI_URL |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + }) |
| 42 | + return mount(Query, { |
| 43 | + ...mountOptions |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +describe('Query view', () => { |
| 48 | + let yasrDiv |
| 49 | + let yasqeDiv |
| 50 | + beforeEach(() => { |
| 51 | + // DOM elements required by YASQE/YASR. |
| 52 | + yasrDiv = document.createElement('div') |
| 53 | + yasrDiv.setAttribute('id', 'yasr') |
| 54 | + yasqeDiv = document.createElement('div') |
| 55 | + yasqeDiv.setAttribute('id', 'yasqe') |
| 56 | + document.body.append(yasrDiv) |
| 57 | + document.body.append(yasqeDiv) |
| 58 | + // we will have to mock setTimeout and nextTick at least, for the component with DOM |
| 59 | + vi.useFakeTimers({ |
| 60 | + toFake: [ |
| 61 | + 'Date', |
| 62 | + 'nextTick', |
| 63 | + 'setTimeout' |
| 64 | + ], |
| 65 | + shouldAdvanceTime: true |
| 66 | + }) |
| 67 | + // jsdom doesn't have getBoundingClientRect |
| 68 | + document.createRange = () => { |
| 69 | + const range = new Range(); |
| 70 | + |
| 71 | + range.getBoundingClientRect = () => { |
| 72 | + return { |
| 73 | + x: 0, |
| 74 | + y: 0, |
| 75 | + bottom: 0, |
| 76 | + height: 0, |
| 77 | + left: 0, |
| 78 | + right: 0, |
| 79 | + top: 0, |
| 80 | + width: 0, |
| 81 | + toJSON: () => {} |
| 82 | + }; |
| 83 | + }; |
| 84 | + |
| 85 | + range.getClientRects = () => { |
| 86 | + return { |
| 87 | + // eslint-disable-next-line no-unused-vars |
| 88 | + item: (index) => null, |
| 89 | + length: 0, |
| 90 | + *[Symbol.iterator](){} |
| 91 | + }; |
| 92 | + }; |
| 93 | + |
| 94 | + return range; |
| 95 | + } |
| 96 | + }) |
| 97 | + afterEach(() => { |
| 98 | + vi.restoreAllMocks() |
| 99 | + vi.useRealTimers() |
| 100 | + }) |
| 101 | + it('is created with the correct initial values', async () => { |
| 102 | + expect(vi.isFakeTimers()).equals(true) |
| 103 | + const datasetName = 'test' |
| 104 | + const wrapper = mountFunction({ |
| 105 | + props: { |
| 106 | + datasetName: datasetName |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + // Test the prop value. |
| 111 | + expect(wrapper.vm.$props.datasetName).equals(datasetName) |
| 112 | + |
| 113 | + // The component needs to interface with DOM due to YASQE, and it contains |
| 114 | + // a `nextTick`, that calls `setTimeout` (this is what worked in the end, |
| 115 | + // although probably a `Teleport` could replace it...). So we need to mock |
| 116 | + // that here. The timeout is of `300ms`, so we move the clock by `400ms`. |
| 117 | + await nextTick() |
| 118 | + await vi.advanceTimersByTime(400) |
| 119 | + await flushPromises() |
| 120 | + |
| 121 | + // Now YASQE and YASR must have been initialized. |
| 122 | + expect(wrapper.vm.yasqe).not.equals(null) |
| 123 | + |
| 124 | + // Test the initial values. |
| 125 | + const yasqeOptions = wrapper.vm.yasqe.options |
| 126 | + expect(yasqeOptions.showQueryButton).true |
| 127 | + expect(yasqeOptions.resizeable).true |
| 128 | + |
| 129 | + const requestConfig = yasqeOptions.requestConfig |
| 130 | + expect(await requestConfig.endpoint).equals(FAKE_FUSEKI_URL) |
| 131 | + // See issue https://github.com/apache/jena/issues/1611 |
| 132 | + expect(requestConfig.acceptHeaderGraph).equals(wrapper.vm.$data.contentTypeGraph) |
| 133 | + }) |
| 134 | +}) |
0 commit comments