Skip to content

Commit

Permalink
Prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
timowestnosto committed Dec 18, 2024
1 parent 382784c commit ab67be9
Show file tree
Hide file tree
Showing 24 changed files with 1,816 additions and 1,844 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"prettier": {
"trailingComma": "es5",
"semi": false,
"arrowParens": "avoid"
"arrowParens": "avoid",
"tabWidth": 2
},
"release": {
"branches": [
Expand Down
88 changes: 43 additions & 45 deletions spec/custom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "@testing-library/jest-dom"
import { autocomplete } from "../src/autocomplete"

beforeAll(() => {
document.body.innerHTML = `
document.body.innerHTML = `
<form id="search-form">
<input type="text" id="search" placeholder="search" data-testid="input"/>
<button type="submit" id="search-button">Search</button>
Expand All @@ -15,56 +15,54 @@ beforeAll(() => {
})

afterAll(() => {
document.body.innerHTML = ""
document.body.innerHTML = ""
})

describe("autocomplete", () => {
it("supports custom fetch function", async () => {
const user = userEvent.setup()
it("supports custom fetch function", async () => {
const user = userEvent.setup()

autocomplete({
inputSelector: "#search",
dropdownSelector: "#search-results",
fetch(input) {
return Promise.resolve({
items: [input, "blue"],
})
},
render: (container, state) => {
container.innerHTML =
state?.items?.length > 0
? state.items
.map(item => `<div>keyword ${item}</div>`)
.join("")
: ""
},
submit: query => {
// Handle search submit
console.log("Submitting search with query: ", query)
},
autocomplete({
inputSelector: "#search",
dropdownSelector: "#search-results",
fetch(input) {
return Promise.resolve({
items: [input, "blue"],
})
},
render: (container, state) => {
container.innerHTML =
state?.items?.length > 0
? state.items.map(item => `<div>keyword ${item}</div>`).join("")
: ""
},
submit: query => {
// Handle search submit
console.log("Submitting search with query: ", query)
},
})

await waitFor(
() => {
expect(screen.getByTestId("dropdown")).not.toBeVisible()
},
{
timeout: 1000,
}
)
await waitFor(
() => {
expect(screen.getByTestId("dropdown")).not.toBeVisible()
},
{
timeout: 1000,
}
)

await user.type(screen.getByTestId("input"), "red")
await user.type(screen.getByTestId("input"), "red")

await waitFor(
() => {
expect(screen.getByTestId("dropdown")).toBeVisible()
},
{
timeout: 4000,
}
)
await waitFor(
() => {
expect(screen.getByTestId("dropdown")).toBeVisible()
},
{
timeout: 4000,
}
)

expect(screen.getByText("keyword red")).toBeVisible()
expect(screen.getByText("keyword blue")).toBeVisible()
})
})
expect(screen.getByText("keyword red")).toBeVisible()
expect(screen.getByText("keyword blue")).toBeVisible()
})
})
96 changes: 48 additions & 48 deletions spec/liquid.spec.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
import "@testing-library/jest-dom"
import {
fromLiquidTemplate,
fromRemoteLiquidTemplate,
defaultLiquidTemplate as liquidTemplate,
fromLiquidTemplate,
fromRemoteLiquidTemplate,
defaultLiquidTemplate as liquidTemplate,
} from "../src/liquid"
import {
handleAutocomplete,
hooks,
autocompleteSuite,
handleAutocomplete,
hooks,
autocompleteSuite,
} from "./suites/autocomplete"
import { waitFor } from "@testing-library/dom"

function libraryScript() {
const liquidScript = document.createElement("script")
liquidScript.src =
"https://cdn.jsdelivr.net/npm/[email protected]/dist/liquid.browser.min.js"
document.body.appendChild(liquidScript)
const liquidScript = document.createElement("script")
liquidScript.src =
"https://cdn.jsdelivr.net/npm/[email protected]/dist/liquid.browser.min.js"
document.body.appendChild(liquidScript)
}

describe("fromLiquidTemplate", () => {
autocompleteSuite({
render: () => fromLiquidTemplate(liquidTemplate),
libraryScript,
})
autocompleteSuite({
render: () => fromLiquidTemplate(liquidTemplate),
libraryScript,
})
})

describe("fromRemoteLiquidTemplate", () => {
hooks(libraryScript)
hooks(libraryScript)

it("fetches remote templates url", async () => {
const openSpy = jest.spyOn(XMLHttpRequest.prototype, "open")
const sendSpy = jest.spyOn(XMLHttpRequest.prototype, "send")
it("fetches remote templates url", async () => {
const openSpy = jest.spyOn(XMLHttpRequest.prototype, "open")
const sendSpy = jest.spyOn(XMLHttpRequest.prototype, "send")

const mockUrl = "template.liquid"
const render = fromRemoteLiquidTemplate(mockUrl)
const mockUrl = "template.liquid"
const render = fromRemoteLiquidTemplate(mockUrl)

const mockXhr = {
open: jest.fn(),
send: jest.fn(),
status: 200,
responseText: liquidTemplate,
onload: jest.fn(),
onerror: jest.fn(),
}
const mockXhr = {
open: jest.fn(),
send: jest.fn(),
status: 200,
responseText: liquidTemplate,
onload: jest.fn(),
onerror: jest.fn(),
}

openSpy.mockImplementation((method, url) => {
if (url === mockUrl) {
return mockXhr.open(method, url)
}
return openSpy.mock.calls[0]
})
openSpy.mockImplementation((method, url) => {
if (url === mockUrl) {
return mockXhr.open(method, url)
}
return openSpy.mock.calls[0]
})

sendSpy.mockImplementation(() => {
return sendSpy.mock.calls[0]
})
sendSpy.mockImplementation(() => {
return sendSpy.mock.calls[0]
})

await waitFor(() => handleAutocomplete(render))
await waitFor(() => handleAutocomplete(render))

await waitFor(
() => {
expect(openSpy).toHaveBeenCalledWith("GET", mockUrl)
expect(sendSpy).toHaveBeenCalled()
},
{
timeout: 1000,
}
)
})
await waitFor(
() => {
expect(openSpy).toHaveBeenCalledWith("GET", mockUrl)
expect(sendSpy).toHaveBeenCalled()
},
{
timeout: 1000,
}
)
})
})
94 changes: 47 additions & 47 deletions spec/mustache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
import "@testing-library/jest-dom"
import {
fromMustacheTemplate,
fromRemoteMustacheTemplate,
defaultMustacheTemplate as mustacheTemplate,
fromMustacheTemplate,
fromRemoteMustacheTemplate,
defaultMustacheTemplate as mustacheTemplate,
} from "../src/mustache"
import {
autocompleteSuite,
handleAutocomplete,
hooks,
autocompleteSuite,
handleAutocomplete,
hooks,
} from "./suites/autocomplete"
import { waitFor } from "@testing-library/dom"

function libraryScript() {
const mustacheScript = document.createElement("script")
mustacheScript.src = "https://unpkg.com/[email protected]/mustache.min.js"
document.body.appendChild(mustacheScript)
const mustacheScript = document.createElement("script")
mustacheScript.src = "https://unpkg.com/[email protected]/mustache.min.js"
document.body.appendChild(mustacheScript)
}

describe("fromMustacheTemplate", () => {
autocompleteSuite({
render: () => fromMustacheTemplate(mustacheTemplate),
libraryScript,
})
autocompleteSuite({
render: () => fromMustacheTemplate(mustacheTemplate),
libraryScript,
})
})

describe("fromRemoteMustacheTemplate", () => {
hooks(libraryScript)
hooks(libraryScript)

it("fetches remote templates url", async () => {
const openSpy = jest.spyOn(XMLHttpRequest.prototype, "open")
const sendSpy = jest.spyOn(XMLHttpRequest.prototype, "send")
it("fetches remote templates url", async () => {
const openSpy = jest.spyOn(XMLHttpRequest.prototype, "open")
const sendSpy = jest.spyOn(XMLHttpRequest.prototype, "send")

const mockUrl = "template.mustache"
const render = fromRemoteMustacheTemplate(mockUrl)
const mockUrl = "template.mustache"
const render = fromRemoteMustacheTemplate(mockUrl)

const mockXhr = {
open: jest.fn(),
send: jest.fn(),
status: 200,
responseText: mustacheTemplate,
onload: jest.fn(),
onerror: jest.fn(),
}
const mockXhr = {
open: jest.fn(),
send: jest.fn(),
status: 200,
responseText: mustacheTemplate,
onload: jest.fn(),
onerror: jest.fn(),
}

openSpy.mockImplementation((method, url) => {
if (url === mockUrl) {
return mockXhr.open(method, url)
}
return openSpy.mock.calls[0]
})
openSpy.mockImplementation((method, url) => {
if (url === mockUrl) {
return mockXhr.open(method, url)
}
return openSpy.mock.calls[0]
})

sendSpy.mockImplementation(() => {
return sendSpy.mock.calls[0]
})
sendSpy.mockImplementation(() => {
return sendSpy.mock.calls[0]
})

await waitFor(() => handleAutocomplete(render))
await waitFor(() => handleAutocomplete(render))

await waitFor(
() => {
expect(openSpy).toHaveBeenCalledWith("GET", mockUrl)
expect(sendSpy).toHaveBeenCalled()
},
{
timeout: 1000,
}
)
})
await waitFor(
() => {
expect(openSpy).toHaveBeenCalledWith("GET", mockUrl)
expect(sendSpy).toHaveBeenCalled()
},
{
timeout: 1000,
}
)
})
})
Loading

0 comments on commit ab67be9

Please sign in to comment.