Skip to content

Commit

Permalink
Merge pull request #994 from basecamp/async-tests
Browse files Browse the repository at this point in the history
Replace callbacks with async/await in tests
  • Loading branch information
Alberto Fernández-Capel authored Sep 22, 2022
2 parents 5e1d351 + 74cfe53 commit fe09966
Show file tree
Hide file tree
Showing 29 changed files with 2,246 additions and 2,571 deletions.
57 changes: 29 additions & 28 deletions src/test/system/attachment_gallery_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,60 @@ import {
assert,
clickToolbarButton,
createImageAttachment,
expectDocument,
insertAttachments,
pressKey,
test,
testGroup,
typeCharacters,
} from "test/test_helper"
import { OBJECT_REPLACEMENT_CHARACTER } from "trix/constants"
import { nextFrame } from "../test_helpers/timing_helpers"

const ORC = OBJECT_REPLACEMENT_CHARACTER

testGroup("Attachment galleries", { template: "editor_empty" }, () => {
test("inserting more than one image attachment creates a gallery block", function (expectDocument) {
test("inserting more than one image attachment creates a gallery block", () => {
insertAttachments(createImageAttachments(2))
assert.blockAttributes([ 0, 2 ], [ "attachmentGallery" ])
expectDocument(`${ORC}${ORC}\n`)
})

test("gallery formatting is removed from blocks containing less than two image attachments", function (expectDocument) {
test("gallery formatting is removed from blocks containing less than two image attachments", async () => {
insertAttachments(createImageAttachments(2))
assert.blockAttributes([ 0, 2 ], [ "attachmentGallery" ])
getEditor().setSelectedRange([ 1, 2 ])
pressKey("backspace", () => {
requestAnimationFrame(() => {
assert.blockAttributes([ 0, 2 ], [])
expectDocument(`${ORC}\n`)
})
})

await pressKey("backspace")
await nextFrame()

assert.blockAttributes([ 0, 2 ], [])
expectDocument(`${ORC}\n`)
})

test("typing in an attachment gallery block splits it", function (expectDocument) {
test("typing in an attachment gallery block splits it", async () => {
insertAttachments(createImageAttachments(4))
getEditor().setSelectedRange(2)
typeCharacters("a", () => {
requestAnimationFrame(() => {
assert.blockAttributes([ 0, 2 ], [ "attachmentGallery" ])
assert.blockAttributes([ 3, 4 ], [])
assert.blockAttributes([ 5, 7 ], [ "attachmentGallery" ])
expectDocument(`${ORC}${ORC}\na\n${ORC}${ORC}\n`)
})
})

await typeCharacters("a")
await nextFrame()

assert.blockAttributes([ 0, 2 ], [ "attachmentGallery" ])
assert.blockAttributes([ 3, 4 ], [])
assert.blockAttributes([ 5, 7 ], [ "attachmentGallery" ])
expectDocument(`${ORC}${ORC}\na\n${ORC}${ORC}\n`)
})

test("inserting a gallery in a formatted block", (expectDocument) => {
clickToolbarButton({ attribute: "quote" }, () => {
typeCharacters("abc", () => {
insertAttachments(createImageAttachments(2))
requestAnimationFrame(() => {
assert.blockAttributes([ 0, 3 ], [ "quote" ])
assert.blockAttributes([ 4, 6 ], [ "attachmentGallery" ])
expectDocument(`abc\n${ORC}${ORC}\n`)
})
})
})
test("inserting a gallery in a formatted block", async () => {
await clickToolbarButton({ attribute: "quote" })
await typeCharacters("abc")

insertAttachments(createImageAttachments(2))
await nextFrame()

assert.blockAttributes([ 0, 3 ], [ "quote" ])
assert.blockAttributes([ 4, 6 ], [ "attachmentGallery" ])
expectDocument(`abc\n${ORC}${ORC}\n`)
})
})

Expand Down
219 changes: 109 additions & 110 deletions src/test/system/attachment_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,143 +2,142 @@ import * as config from "trix/config"
import { OBJECT_REPLACEMENT_CHARACTER } from "trix/constants"

import {
after,
assert,
clickElement,
clickToolbarButton,
createFile,
defer,
dragToCoordinates,
expectDocument,
moveCursor,
pressKey,
test,
testGroup,
triggerEvent,
typeCharacters,
} from "test/test_helper"
import { delay, nextFrame } from "../test_helpers/timing_helpers"

testGroup("Attachments", { template: "editor_with_image" }, () => {
test("moving an image by drag and drop", (expectDocument) => {
typeCharacters("!", () => {
moveCursor({ direction: "right", times: 1 }, (coordinates) => {
const img = document.activeElement.querySelector("img")
triggerEvent(img, "mousedown")
defer(() => {
dragToCoordinates(coordinates, () => {
expectDocument(`!a${OBJECT_REPLACEMENT_CHARACTER}b\n`)
})
})
})
})
test("moving an image by drag and drop", async () => {
await typeCharacters("!")

const coordinates = await moveCursor({ direction: "right", times: 1 })
const img = document.activeElement.querySelector("img")
triggerEvent(img, "mousedown")

await nextFrame()
await dragToCoordinates(coordinates)

expectDocument(`!a${OBJECT_REPLACEMENT_CHARACTER}b\n`)
})

test("removing an image", (expectDocument) => {
after(20, () => {
clickElement(getFigure(), () => {
const closeButton = getFigure().querySelector("[data-trix-action=remove]")
clickElement(closeButton, () => {
expectDocument("ab\n")
})
})
})
test("removing an image", async () => {
await delay(20)
await clickElement(getFigure())

const closeButton = getFigure().querySelector("[data-trix-action=remove]")
await clickElement(closeButton)

expectDocument("ab\n")
})

test("editing an image caption", (expectDocument) => {
after(20, () => {
clickElement(findElement("figure"), () => {
clickElement(findElement("figcaption"), () => {
defer(() => {
const textarea = findElement("textarea")
assert.ok(textarea)
textarea.focus()
textarea.value = "my"
triggerEvent(textarea, "input")
defer(() => {
textarea.value = ""
defer(() => {
textarea.value = "my caption"
triggerEvent(textarea, "input")
pressKey("return", () => {
assert.notOk(findElement("textarea"))
assert.textAttributes([ 2, 3 ], { caption: "my caption" })
assert.locationRange({ index: 0, offset: 3 })
expectDocument(`ab${OBJECT_REPLACEMENT_CHARACTER}\n`)
})
})
})
})
})
})
})
test("editing an image caption", async () => {
await delay(20)

await clickElement(findElement("figure"))
await clickElement(findElement("figcaption"))

await nextFrame()

const textarea = findElement("textarea")
assert.ok(textarea)

textarea.focus()
textarea.value = "my"
triggerEvent(textarea, "input")

await nextFrame()
textarea.value = ""

await nextFrame()
textarea.value = "my caption"
triggerEvent(textarea, "input")

await pressKey("return")
assert.notOk(findElement("textarea"))
assert.textAttributes([ 2, 3 ], { caption: "my caption" })
assert.locationRange({ index: 0, offset: 3 })
expectDocument(`ab${OBJECT_REPLACEMENT_CHARACTER}\n`)
})

test("editing an attachment caption with no filename", (done) =>
after(20, () => {
let captionElement = findElement("figcaption")
assert.ok(captionElement.clientHeight > 0)
assert.equal(captionElement.getAttribute("data-trix-placeholder"), config.lang.captionPlaceholder)

clickElement(findElement("figure"), () => {
captionElement = findElement("figcaption")
assert.ok(captionElement.clientHeight > 0)
assert.equal(captionElement.getAttribute("data-trix-placeholder"), config.lang.captionPlaceholder)
done()
})
}))

test("updating an attachment's href attribute while editing its caption", (expectDocument) => {
test("editing an attachment caption with no filename", async () => {
await delay(20)

let captionElement = findElement("figcaption")
assert.ok(captionElement.clientHeight > 0)
assert.equal(captionElement.getAttribute("data-trix-placeholder"), config.lang.captionPlaceholder)

await clickElement(findElement("figure"))

captionElement = findElement("figcaption")
assert.ok(captionElement.clientHeight > 0)
assert.equal(captionElement.getAttribute("data-trix-placeholder"), config.lang.captionPlaceholder)
})

test("updating an attachment's href attribute while editing its caption", async () => {
const attachment = getEditorController().attachmentManager.getAttachments()[0]

after(20, () => {
clickElement(findElement("figure"), () => {
clickElement(findElement("figcaption"), () => {
defer(() => {
let textarea = findElement("textarea")
assert.ok(textarea)
textarea.focus()
textarea.value = "my caption"
triggerEvent(textarea, "input")
attachment.setAttributes({ href: "https://example.com" })
defer(() => {
textarea = findElement("textarea")
assert.ok(document.activeElement === textarea)
assert.equal(textarea.value, "my caption")
pressKey("return", () => {
assert.notOk(findElement("textarea"))
assert.textAttributes([ 2, 3 ], { caption: "my caption" })
assert.locationRange({ index: 0, offset: 3 })
expectDocument(`ab${OBJECT_REPLACEMENT_CHARACTER}\n`)
})
})
})
})
})
})
await delay(20)

await clickElement(findElement("figure"))
await clickElement(findElement("figcaption"))

await nextFrame()

let textarea = findElement("textarea")
assert.ok(textarea)
textarea.focus()
textarea.value = "my caption"
triggerEvent(textarea, "input")
attachment.setAttributes({ href: "https://example.com" })

await nextFrame()

textarea = findElement("textarea")
assert.ok(document.activeElement === textarea)
assert.equal(textarea.value, "my caption")

await pressKey("return")

assert.notOk(findElement("textarea"))
assert.textAttributes([ 2, 3 ], { caption: "my caption" })
assert.locationRange({ index: 0, offset: 3 })

expectDocument(`ab${OBJECT_REPLACEMENT_CHARACTER}\n`)
})

testGroup("File insertion", { template: "editor_empty" }, () => {
test("inserting a file in a formatted block", (expectDocument) => {
clickToolbarButton({ attribute: "bullet" }, () => {
clickToolbarButton({ attribute: "bold" }, () => {
getComposition().insertFile(createFile())
assert.blockAttributes([ 0, 1 ], [ "bulletList", "bullet" ])
assert.textAttributes([ 0, 1 ], {})
expectDocument(`${OBJECT_REPLACEMENT_CHARACTER}\n`)
})
})
test("inserting a file in a formatted block", async () => {
await clickToolbarButton({ attribute: "bullet" })
await clickToolbarButton({ attribute: "bold" })

getComposition().insertFile(createFile())

assert.blockAttributes([ 0, 1 ], [ "bulletList", "bullet" ])
assert.textAttributes([ 0, 1 ], {})
expectDocument(`${OBJECT_REPLACEMENT_CHARACTER}\n`)
})

test("inserting a files in a formatted block", (expectDocument) => {
clickToolbarButton({ attribute: "quote" }, () => {
clickToolbarButton({ attribute: "italic" }, () => {
getComposition().insertFiles([ createFile(), createFile() ])

assert.blockAttributes([ 0, 2 ], [ "quote" ])
assert.textAttributes([ 0, 1 ], {})
assert.textAttributes([ 1, 2 ], {})
expectDocument(`${OBJECT_REPLACEMENT_CHARACTER}${OBJECT_REPLACEMENT_CHARACTER}\n`)
})
})
test("inserting a files in a formatted block", async () => {
await clickToolbarButton({ attribute: "quote" })
await clickToolbarButton({ attribute: "italic" })

getComposition().insertFiles([ createFile(), createFile() ])

assert.blockAttributes([ 0, 2 ], [ "quote" ])
assert.textAttributes([ 0, 1 ], {})
assert.textAttributes([ 1, 2 ], {})
expectDocument(`${OBJECT_REPLACEMENT_CHARACTER}${OBJECT_REPLACEMENT_CHARACTER}\n`)
})
})
})
Expand Down
Loading

0 comments on commit fe09966

Please sign in to comment.