test: consolidate unit tests (#9588)

All mocha tests have been migrated to Jest tests.

Closes #9564
This commit is contained in:
Vladimír Gorej
2024-02-14 11:39:40 +01:00
committed by GitHub
parent 8b0991c5cc
commit 7addbd0eb5
19 changed files with 27 additions and 331 deletions

View File

@@ -1,7 +1,7 @@
env:
mocha: true
"jest/globals": true
rules:
"react/prop-types": 1 # bah humbug
"react/require-render-return": 1
"no-unused-vars": 1 # unused vars in tests can be useful for indicating a full signature
"no-global-assign": 1
"no-global-assign": 1

View File

@@ -1,41 +0,0 @@
import React from "react"
import expect from "expect"
import { shallow } from "enzyme"
import ResponseBody from "core/components/response-body"
describe("<ResponseBody />", function() {
const highlightCodeComponent = () => null
const components = {
highlightCode: highlightCodeComponent
}
const props = {
getComponent: c => components[c],
}
it("renders ResponseBody as 'application/json'", function() {
props.contentType = "application/json"
props.content = "{\"key\": \"a test value\"}"
const wrapper = shallow(<ResponseBody {...props}/>)
expect(wrapper.find("highlightCodeComponent").length).toEqual(1)
})
it("renders ResponseBody as 'text/html'", function() {
props.contentType = "application/json"
props.content = "<b>Result</b>"
const wrapper = shallow(<ResponseBody {...props}/>)
expect(wrapper.find("highlightCodeComponent").length).toEqual(1)
})
it("renders ResponseBody as 'image/svg'", function() {
props.contentType = "image/svg"
const wrapper = shallow(<ResponseBody {...props}/>)
expect(wrapper.find("highlightCodeComponent").length).toEqual(0)
})
it("should render a copyable highlightCodeComponent for text types", function() {
props.contentType = "text/plain"
props.content = "test text"
const wrapper = shallow(<ResponseBody {...props}/>)
expect(wrapper.find("highlightCodeComponent[canCopy]").length).toEqual(1)
})
})

View File

@@ -1,7 +0,0 @@
env:
mocha: true
rules:
"react/prop-types": 1 # bah humbug
"react/require-render-return": 1
"no-unused-vars": 1 # unused vars in tests can be useful for indicating a full signature
"no-global-assign": 1

View File

@@ -1,104 +0,0 @@
/* eslint-env mocha */
import React from "react"
import { fromJSOrdered } from "core/utils"
import sinon from "sinon"
import expect from "expect"
import { shallow } from "enzyme"
import LiveResponse from "core/components/live-response"
import ResponseBody from "core/components/response-body"
import RequestSnippets from "core/plugins/request-snippets/request-snippets"
describe("<LiveResponse/>", function () {
let request = fromJSOrdered({
credentials: "same-origin",
headers: {
accept: "application/xml"
},
url: "http://petstore.swagger.io/v2/pet/1"
})
let mutatedRequest = fromJSOrdered({
credentials: "same-origin",
headers: {
accept: "application/xml",
mutated: "header"
},
url: "http://mutated.petstore.swagger.io/v2/pet/1"
})
let requests = {
request: request,
mutatedRequest: mutatedRequest
}
const tests = [
{ showMutatedRequest: true, expected: { request: "mutatedRequest", requestForCalls: 0, mutatedRequestForCalls: 1 } },
{ showMutatedRequest: false, expected: { request: "request", requestForCalls: 1, mutatedRequestForCalls: 0 } }
]
tests.forEach(function (test) {
it("passes " + test.expected.request + " to RequestSnippets when showMutatedRequest = " + test.showMutatedRequest, function () {
// Given
let response = fromJSOrdered({
status: 200,
url: "http://petstore.swagger.io/v2/pet/1",
headers: {
"content-type": "application/xml"
},
text: "<response/>",
duration: 50
})
let mutatedRequestForSpy = sinon.stub().returns(mutatedRequest)
let requestForSpy = sinon.stub().returns(request)
let components = {
RequestSnippets: RequestSnippets,
responseBody: ResponseBody
}
let props = {
response: response,
specSelectors: {
mutatedRequestFor: mutatedRequestForSpy,
requestFor: requestForSpy,
},
pathMethod: ["/one", "get"],
getComponent: (c) => {
return components[c]
},
displayRequestDuration: true,
getConfigs: () => ({ showMutatedRequest: test.showMutatedRequest, requestSnippetsEnabled: true })
}
// When
let wrapper = shallow(<LiveResponse {...props} />)
// Then
expect(mutatedRequestForSpy.callCount).toEqual(test.expected.mutatedRequestForCalls)
expect(requestForSpy.callCount).toEqual(test.expected.requestForCalls)
const snippets = wrapper.find("RequestSnippets")
expect(snippets.length).toEqual(1)
expect(snippets.props().request).toBe(requests[test.expected.request])
const expectedUrl = requests[test.expected.request].get("url")
expect(wrapper.find("div.request-url pre.microlight").text()).toEqual(expectedUrl)
let duration = wrapper.find("Duration")
expect(duration.length).toEqual(1)
expect(duration.props().duration).toEqual(50)
expect(duration.html())
.toEqual("<div><h5>Request duration</h5><pre class=\"microlight\">50 ms</pre></div>")
let responseHeaders = wrapper.find("Headers")
expect(duration.length).toEqual(1)
expect(responseHeaders.props().headers.length).toEqual(1)
expect(responseHeaders.props().headers[0].key).toEqual("content-type")
expect(responseHeaders.html())
.toEqual("<div><h5>Response headers</h5><pre class=\"microlight\"><span class=\"headerline\"> content-type: application/xml </span></pre></div>")
})
})
})

View File

@@ -1,79 +0,0 @@
/* eslint-env mocha */
import React from "react"
import expect from "expect"
import { mount } from "enzyme"
import { fromJS, Map } from "immutable"
import OnlineValidatorBadge from "core/components/online-validator-badge"
describe("<OnlineValidatorBadge/>", function () {
it("should render a validator link and image correctly for the default validator", function () {
// When
const props = {
getConfigs: () => ({}),
getComponent: () => null,
specSelectors: {
url: () => "swagger.json"
}
}
const wrapper = mount(
<OnlineValidatorBadge {...props} />
)
// Then
expect(wrapper.find("a").props().href).toEqual(
"https://validator.swagger.io/validator/debug?url=swagger.json"
)
expect(wrapper.find("ValidatorImage").length).toEqual(1)
expect(wrapper.find("ValidatorImage").props().src).toEqual(
"https://validator.swagger.io/validator?url=swagger.json"
)
})
it("should encode a definition URL correctly", function () {
// When
const props = {
getConfigs: () => ({}),
getComponent: () => null,
specSelectors: {
url: () => "http://google.com/swagger.json"
}
}
const wrapper = mount(
<OnlineValidatorBadge {...props} />
)
// Then
expect(wrapper.find("a").props().href).toEqual(
"https://validator.swagger.io/validator/debug?url=http%3A%2F%2Fgoogle.com%2Fswagger.json"
)
expect(wrapper.find("ValidatorImage").length).toEqual(1)
expect(wrapper.find("ValidatorImage").props().src).toEqual(
"https://validator.swagger.io/validator?url=http%3A%2F%2Fgoogle.com%2Fswagger.json"
)
})
it.skip("should resolve a definition URL against the browser's location", function () {
// TODO: mock `window`
// When
const props = {
getConfigs: () => ({}),
getComponent: () => null,
specSelectors: {
url: () => "http://google.com/swagger.json"
}
}
const wrapper = mount(
<OnlineValidatorBadge {...props} />
)
// Then
expect(wrapper.find("a").props().href).toEqual(
"https://validator.swagger.io/validator/debug?url=http%3A%2F%2Fgoogle.com%2Fswagger.json"
)
expect(wrapper.find("ValidatorImage").length).toEqual(1)
expect(wrapper.find("ValidatorImage").props().src).toEqual(
"https://validator.swagger.io/validator?url=http%3A%2F%2Fgoogle.com%2Fswagger.json"
)
})
// should resolve a definition URL against the browser's location
})

View File

@@ -1,28 +0,0 @@
const { JSDOM } = require("jsdom")
const Enzyme = require("enzyme")
const { default: Adapter } = require("@cfaester/enzyme-adapter-react-18")
const win = require("../../src/core/window")
Enzyme.configure({ adapter: new Adapter() })
const jsdom = new JSDOM("<!doctype html><html><body></body></html>")
const { window } = jsdom
function copyProps(src, target) {
const props = Object.getOwnPropertyNames(src)
.filter(prop => typeof target[prop] === "undefined")
.reduce((result, prop) => ({
...result,
[prop]: Object.getOwnPropertyDescriptor(src, prop),
}), {})
Object.defineProperties(target, props)
}
global.window = window
global.document = window.document
global.navigator = {
userAgent: "node.js",
}
copyProps(win, window) // use UI's built-in window wrapper
copyProps(window, global)

View File

@@ -1,32 +0,0 @@
/* eslint-env mocha */
import React from "react"
import expect from "expect"
import { mount } from "enzyme"
import { fromJS, Map } from "immutable"
import OnlineValidatorBadge from "core/components/online-validator-badge"
describe("<OnlineValidatorBadge/> Anchor Target Safety", function () {
it("should render a validator link with safe `rel` attributes", function () {
// When
const props = {
getConfigs: () => ({}),
getComponent: () => null,
specSelectors: {
url: () => "swagger.json"
}
}
const wrapper = mount(
<OnlineValidatorBadge {...props} />
)
const anchor = wrapper.find("a")
// Then
expect(anchor.props().href).toEqual(
"https://validator.swagger.io/validator/debug?url=swagger.json"
)
expect(anchor.props().target).toEqual("_blank")
expect(anchor.props().rel || "").toContain("noopener")
expect(anchor.props().rel || "").toContain("noreferrer")
})
})

View File

View File

@@ -1,6 +1,6 @@
import React from "react"
import expect from "expect"
import { shallow } from "enzyme"
import { shallow, mount } from "enzyme"
import HighlightCode from "core/components/highlight-code"
const fakeGetConfigs = () => ({syntaxHighlight: {activated: true, theme: "agate"}})
@@ -21,10 +21,10 @@ describe("<HighlightCode />", () => {
it("should render values in a preformatted element", () => {
const value = "test text"
const props = {value: value, getConfigs: fakeGetConfigs}
const wrapper = shallow(<HighlightCode {...props} />)
const wrapper = mount(<HighlightCode {...props} />)
const preTag = wrapper.find("pre")
expect(preTag.length).toEqual(1)
expect(preTag.contains(value)).toEqual(true)
expect(preTag.text()).toEqual(value)
})
})

View File

@@ -48,8 +48,8 @@ describe("<LiveResponse/>", function(){
duration: 50
})
let mutatedRequestForSpy = jest.fn().mockImplementation(function(mutatedRequest) { return mutatedRequest })
let requestForSpy = jest.fn().mockImplementation(function(request) { return request })
let mutatedRequestForSpy = jest.fn().mockImplementation(function() { return mutatedRequest })
let requestForSpy = jest.fn().mockImplementation(function() { return request })
let components = {
curl: Curl,
@@ -74,8 +74,8 @@ describe("<LiveResponse/>", function(){
let wrapper = shallow(<LiveResponse {...props}/>)
// Then
expect(mutatedRequestForSpy.calls.length).toEqual(test.expected.mutatedRequestForCalls)
expect(requestForSpy.calls.length).toEqual(test.expected.requestForCalls)
expect(mutatedRequestForSpy.mock.calls.length).toEqual(test.expected.mutatedRequestForCalls)
expect(requestForSpy.mock.calls.length).toEqual(test.expected.requestForCalls)
const curl = wrapper.find(Curl)
expect(curl.length).toEqual(1)
@@ -84,13 +84,13 @@ describe("<LiveResponse/>", function(){
const expectedUrl = requests[test.expected.request].get("url")
expect(wrapper.find("div.request-url pre.microlight").text()).toEqual(expectedUrl)
let duration = wrapper.find("Duration")
const duration = wrapper.find("Duration")
expect(duration.length).toEqual(1)
expect(duration.props().duration).toEqual(50)
expect(duration.html())
.toEqual("<div><h5>Request duration</h5><pre class=\"microlight\">50 ms</pre></div>")
let responseHeaders = wrapper.find("Headers")
const responseHeaders = wrapper.find("Headers")
expect(duration.length).toEqual(1)
expect(responseHeaders.props().headers.length).toEqual(1)
expect(responseHeaders.props().headers[0].key).toEqual("content-type")

View File

@@ -1,6 +1,7 @@
import React from "react"
import { mount } from "enzyme"
import OnlineValidatorBadge from "core/components/online-validator-badge"
import expect from "expect"
describe("<OnlineValidatorBadge/>", function () {
it("should render a validator link and image correctly for the default validator", function () {
@@ -9,7 +10,7 @@ describe("<OnlineValidatorBadge/>", function () {
getConfigs: () => ({}),
getComponent: () => null,
specSelectors: {
url: () => "swagger.json"
url: () => "https://smartbear.com/swagger.json"
}
}
const wrapper = mount(
@@ -18,13 +19,11 @@ describe("<OnlineValidatorBadge/>", function () {
// Then
expect(wrapper.find("a").props().href).toEqual(
"https://validator.swagger.io/validator/debug?url=swagger.json"
"https://validator.swagger.io/validator/debug?url=https%3A%2F%2Fsmartbear.com%2Fswagger.json"
)
expect(wrapper.find("ValidatorImage").length).toEqual(1)
expect(wrapper.find("ValidatorImage").props().src).toEqual(
"https://validator.swagger.io/validator?url=swagger.json"
)
})
it("should encode a definition URL correctly", function () {
// When
const props = {
@@ -47,7 +46,8 @@ describe("<OnlineValidatorBadge/>", function () {
"https://validator.swagger.io/validator?url=http%3A%2F%2Fgoogle.com%2Fswagger.json"
)
})
it.skip("should resolve a definition URL against the browser's location", function () {
it("should resolve a definition URL against the browser's location", function () {
// TODO: mock `window`
// When
@@ -72,5 +72,4 @@ describe("<OnlineValidatorBadge/>", function () {
)
})
// should resolve a definition URL against the browser's location
})

View File

@@ -9,7 +9,7 @@ describe("<OnlineValidatorBadge/> Anchor Target Safety", function () {
getConfigs: () => ({}),
getComponent: () => null,
specSelectors: {
url: () => "swagger.json"
url: () => "https://smartbear.com/swagger.json"
}
}
const wrapper = mount(
@@ -20,10 +20,10 @@ describe("<OnlineValidatorBadge/> Anchor Target Safety", function () {
// Then
expect(anchor.props().href).toEqual(
"https://validator.swagger.io/validator/debug?url=swagger.json"
"https://validator.swagger.io/validator/debug?url=https%3A%2F%2Fsmartbear.com%2Fswagger.json"
)
expect(anchor.props().target).toEqual("_blank")
expect(anchor.props().rel || "").toInclude("noopener")
expect(anchor.props().rel || "").toInclude("noreferrer")
expect(anchor.props().rel || "").toContain("noopener")
expect(anchor.props().rel || "").toContain("noreferrer")
})
})