kinow commented on code in PR #2527:
URL: https://github.com/apache/jena/pull/2527#discussion_r1632305942


##########
jena-fuseki2/jena-fuseki-ui/tests/unit/views/dataset/query.vue.spec.js:
##########
@@ -0,0 +1,134 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import { flushPromises, mount } from '@vue/test-utils'
+import { nextTick } from 'vue'
+import Query from '@/views/dataset/Query.vue'
+import {  vi } from 'vitest'
+
+const FAKE_FUSEKI_URL = 'https://localhost:1234/fuseki/'
+
+const $routeMock = {
+  query: {}
+}
+
+const mountFunction = options => {
+  const mountOptions = Object.assign(options || {}, {
+    shallow: true,
+    global: {
+      mocks: {
+        $route: $routeMock,
+        $fusekiService: {
+          getFusekiUrl () {
+            return FAKE_FUSEKI_URL
+          }
+        }
+      }
+    }
+  })
+  return mount(Query, {
+    ...mountOptions
+  })
+}
+
+describe('Query view', () => {
+  let yasrDiv
+  let yasqeDiv
+  beforeEach(() => {
+    // DOM elements required by YASQE/YASR.
+    yasrDiv = document.createElement('div')
+    yasrDiv.setAttribute('id', 'yasr')
+    yasqeDiv = document.createElement('div')
+    yasqeDiv.setAttribute('id', 'yasqe')
+    document.body.append(yasrDiv)
+    document.body.append(yasqeDiv)
+    // we will have to mock setTimeout and nextTick at least, for the 
component with DOM
+    vi.useFakeTimers({
+      toFake: [
+        'Date',
+        'nextTick',
+        'setTimeout'
+      ],
+      shouldAdvanceTime: true
+    })
+    // jsdom doesn't have getBoundingClientRect
+    document.createRange = () => {
+      const range = new Range();
+
+      range.getBoundingClientRect = () => {
+        return {
+          x: 0,
+          y: 0,
+          bottom: 0,
+          height: 0,
+          left: 0,
+          right: 0,
+          top: 0,
+          width: 0,
+          toJSON: () => {}
+        };
+      };
+
+      range.getClientRects = () => {
+        return {
+          // eslint-disable-next-line no-unused-vars
+          item: (index) => null,
+          length: 0,
+          *[Symbol.iterator](){}
+        };
+      };
+
+      return range;
+    }
+  })
+  afterEach(() => {
+    vi.restoreAllMocks()
+    vi.useRealTimers()
+  })
+  it('is created with the correct initial values', async () => {
+    expect(vi.isFakeTimers()).equals(true)
+    const datasetName = 'test'
+    const wrapper = mountFunction({
+      props: {
+        datasetName: datasetName
+      }
+    })
+
+    // Test the prop value.
+    expect(wrapper.vm.$props.datasetName).equals(datasetName)
+
+    // The component needs to interface with DOM due to YASQE, and it contains
+    // a `nextTick`, that calls `setTimeout` (this is what worked in the end,
+    // although probably a `Teleport` could replace it...). So we need to mock
+    // that here. The timeout is of `300ms`, so we move the clock by `400ms`.
+    await nextTick()
+    await vi.advanceTimersByTime(400)
+    await flushPromises()
+
+    // Now YASQE and YASR must have been initialized.
+    expect(wrapper.vm.yasqe).not.equals(null)
+
+    // Test the initial values.
+    const yasqeOptions = wrapper.vm.yasqe.options
+    expect(yasqeOptions.showQueryButton).true
+    expect(yasqeOptions.resizeable).true
+
+    const requestConfig = yasqeOptions.requestConfig
+    expect(await requestConfig.endpoint).equals(FAKE_FUSEKI_URL)
+    // See issue https://github.com/apache/jena/issues/1611
+    
expect(requestConfig.acceptHeaderGraph).equals(wrapper.vm.$data.contentTypeGraph)

Review Comment:
   Normally views are tested with e2e, but since the logic for YASQE/YASR is 
not well separated from the view, ended up having to write a unit test (will 
fix it someday).
   
   The rest of the code is just preparing functions to mount the component, 
mocking things that are not available without a browser (like the 
`getBoundingClientRect` that's missing from jsdom), etc.
   
   Then above this test verifies that the initial values are provided to YASQE< 
including the one for the accept header, which should guarantee we are defining 
it and YAS/R/QE/GUI should all use work as expected.
   
   I haven't verified that the behaviour @afs reported is fixed (ran out of 
time today), but I can do that later, or maybe @afs can check it.
   
   I pushed an extra commit with this test and with a formatting issue for 
spaces before colon (for consistency). Feel free to amend if there's anything 
wrong, @OyvindLGjesdal . And thanks for the PR!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@jena.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@jena.apache.org
For additional commands, e-mail: pr-h...@jena.apache.org

Reply via email to