Pinafore-Web-Client-Frontend/tests/utils.js

242 lines
7.9 KiB
JavaScript
Raw Normal View History

2018-02-20 02:04:37 +01:00
import { ClientFunction as exec, Selector as $ } from 'testcafe'
2018-03-03 02:54:38 +01:00
import * as images from './images'
import * as blobUtils from './blobUtils'
2018-02-20 02:04:37 +01:00
2018-03-16 01:33:52 +01:00
const SCROLL_INTERVAL = 1
2018-02-24 23:49:28 +01:00
2018-02-20 02:04:37 +01:00
export const settingsButton = $('nav a[aria-label=Settings]')
export const instanceInput = $('#instanceInput')
2018-03-27 09:02:55 +02:00
export const modalDialog = $('#modal-dialog')
2018-02-21 18:26:22 +01:00
export const modalDialogContents = $('.modal-dialog-contents')
export const closeDialogButton = $('.close-dialog-button')
2018-02-24 23:49:28 +01:00
export const notificationsNavButton = $('nav a[href="/notifications"]')
export const homeNavButton = $('nav a[href="/"]')
2018-03-19 18:09:05 +01:00
export const localTimelineNavButton = $('nav a[href="/local"]')
2018-03-09 03:08:14 +01:00
export const searchNavButton = $('nav a[href="/search"]')
2018-02-24 23:49:28 +01:00
export const formError = $('.form-error-user-error')
2018-02-28 06:20:48 +01:00
export const composeInput = $('.compose-box-input')
2018-03-04 01:12:48 +01:00
export const composeContentWarning = $('.content-warning-input')
2018-02-28 06:20:48 +01:00
export const composeButton = $('.compose-box-button')
export const composeLengthIndicator = $('.compose-box-length')
2018-03-01 03:45:29 +01:00
export const emojiButton = $('.compose-box-toolbar button:first-child')
2018-03-03 06:55:04 +01:00
export const mediaButton = $('.compose-box-toolbar button:nth-child(2)')
2018-03-03 22:23:26 +01:00
export const postPrivacyButton = $('.compose-box-toolbar button:nth-child(3)')
2018-03-04 01:12:48 +01:00
export const contentWarningButton = $('.compose-box-toolbar button:nth-child(4)')
2018-03-01 07:45:42 +01:00
export const emailInput = $('input#user_email')
export const passwordInput = $('input#user_password')
export const authorizeInput = $('button[type=submit]:not(.negative)')
2018-03-07 08:57:06 +01:00
export const logInToInstanceLink = $('a[href="/settings/instances/add"]')
2018-03-09 03:08:14 +01:00
export const searchInput = $('.search-input')
2018-03-10 07:31:26 +01:00
export const postStatusButton = $('.compose-box-button')
2018-03-10 19:54:16 +01:00
export const showMoreButton = $('.more-items-header button')
2018-03-15 06:14:06 +01:00
export const accountProfileName = $('.account-profile .account-profile-name')
export const accountProfileUsername = $('.account-profile .account-profile-username')
export const accountProfileFollowedBy = $('.account-profile .account-profile-followed-by')
export const accountProfileFollowButton = $('.account-profile .account-profile-follow button')
2018-03-16 04:31:58 +01:00
export const goBackButton = $('.dynamic-page-go-back')
2018-02-24 23:49:28 +01:00
export const favoritesCountElement = $('.status-favs-reblogs:nth-child(3)').addCustomDOMProperties({
innerCount: el => parseInt(el.innerText, 10)
})
export const reblogsCountElement = $('.status-favs-reblogs:nth-child(2)').addCustomDOMProperties({
innerCount: el => parseInt(el.innerText, 10)
})
2018-02-20 02:04:37 +01:00
2018-03-10 19:54:16 +01:00
export const sleep = timeout => new Promise(resolve => setTimeout(resolve, timeout))
2018-02-20 02:04:37 +01:00
export const getUrl = exec(() => window.location.href)
2018-02-21 18:26:22 +01:00
export const getActiveElementClass = exec(() =>
document.activeElement ? document.activeElement.getAttribute('class') : ''
)
2018-03-16 04:31:58 +01:00
export const getActiveElementInnerText = exec(() =>
document.activeElement && document.activeElement.innerText
)
2018-03-17 03:04:48 +01:00
export const getActiveElementAriaLabel = exec(() =>
document.activeElement ? document.activeElement.getAttribute('aria-label') : ''
)
export const getActiveElementInsideNthStatus = exec(() => {
let element = document.activeElement
while (element) {
if (element.hasAttribute('aria-posinset')) {
return element.getAttribute('aria-posinset')
}
element = element.parentElement
}
})
2018-02-21 18:26:22 +01:00
export const goBack = exec(() => window.history.back())
2018-03-09 03:08:14 +01:00
export const forceOffline = exec(() => window.store.set({online: false}))
export const forceOnline = exec(() => window.store.set({online: true}))
2018-03-01 03:45:29 +01:00
export const getComposeSelectionStart = exec(() => composeInput().selectionStart, {
dependencies: { composeInput }
})
2018-03-10 19:54:16 +01:00
export const scrollContainerToTop = exec(() => {
document.getElementsByClassName('container')[0].scrollTop = 0
})
2018-03-03 06:55:04 +01:00
export const uploadKittenImage = i => (exec(() => {
let image = images[`kitten${i}`]
let blob = blobUtils.base64StringToBlob(image.data, 'image/png')
blob.name = image.name
2018-03-03 02:54:38 +01:00
window.__fakeFileInput(blob)
}, {
dependencies: {
images,
2018-03-03 06:55:04 +01:00
blobUtils,
i
2018-03-03 02:54:38 +01:00
}
2018-03-03 06:55:04 +01:00
}))
2018-03-30 10:06:17 +02:00
export function getNthComposeReplyInput (n) {
return getNthStatus(n).find('.compose-box-input')
}
export function getNthComposeReplyButton (n) {
return getNthStatus(n).find('.compose-box-button')
}
2018-03-30 19:04:35 +02:00
export function getNthPostPrivacyButton (n) {
return getNthStatus(n).find('.compose-box-toolbar button:nth-child(3)')
}
2018-03-25 21:24:38 +02:00
export function getNthAutosuggestionResult (n) {
return $(`.compose-autosuggest-list-item:nth-child(${n}) button`)
}
2018-03-09 03:08:14 +01:00
export function getNthSearchResult (n) {
return $(`.search-result:nth-child(${n}) a`)
}
2018-03-03 06:55:04 +01:00
export function getNthMedia (n) {
return $(`.compose-media:nth-child(${n}) img`)
}
2018-03-03 02:54:38 +01:00
2018-03-03 19:27:14 +01:00
export function getNthDeleteMediaButton (n) {
return $(`.compose-media:nth-child(${n}) .compose-media-delete-button`)
}
2018-02-21 06:08:26 +01:00
export function getNthStatus (n) {
2018-02-24 23:49:28 +01:00
return $(`div[aria-hidden="false"] > article[aria-posinset="${n}"]`)
2018-02-21 06:08:26 +01:00
}
export function getLastVisibleStatus () {
2018-02-24 23:49:28 +01:00
return $(`div[aria-hidden="false"] > article[aria-posinset]`).nth(-1)
}
export function getFirstVisibleStatus () {
return $(`div[aria-hidden="false"] > article[aria-posinset]`).nth(0)
}
2018-03-09 17:45:12 +01:00
export function getNthReplyButton (n) {
return getNthStatus(n).find('.status-toolbar button:nth-child(1)')
}
2018-02-24 23:49:28 +01:00
export function getNthFavoriteButton (n) {
return getNthStatus(n).find('.status-toolbar button:nth-child(3)')
}
export function getNthFavorited (n) {
return getNthFavoriteButton(n).getAttribute('aria-pressed')
}
export function getFavoritesCount () {
return favoritesCountElement.innerCount
}
2018-02-25 03:20:33 +01:00
export function getNthReblogButton (n) {
return getNthStatus(n).find('.status-toolbar button:nth-child(2)')
}
export function getNthReblogged (n) {
return getNthReblogButton(n).getAttribute('aria-pressed')
}
2018-02-24 23:49:28 +01:00
export function getReblogsCount () {
return reblogsCountElement.innerCount
2018-02-20 02:04:37 +01:00
}
2018-02-20 03:25:59 +01:00
export async function validateTimeline (t, timeline) {
2018-02-20 02:04:37 +01:00
for (let i = 0; i < timeline.length; i++) {
let status = timeline[i]
if (status.content) {
2018-02-21 06:08:26 +01:00
await t.expect(getNthStatus(i).find('.status-content p').innerText)
2018-02-20 02:04:37 +01:00
.contains(status.content)
}
if (status.spoiler) {
2018-02-21 06:08:26 +01:00
await t.expect(getNthStatus(i).find('.status-spoiler p').innerText)
2018-02-20 02:04:37 +01:00
.contains(status.spoiler)
}
if (status.followedBy) {
2018-02-21 06:08:26 +01:00
await t.expect(getNthStatus(i).find('.status-header span').innerText)
2018-02-20 02:04:37 +01:00
.contains(status.followedBy + ' followed you')
}
if (status.rebloggedBy) {
2018-02-21 06:08:26 +01:00
await t.expect(getNthStatus(i).find('.status-header span').innerText)
2018-02-20 02:04:37 +01:00
.contains(status.rebloggedBy + ' boosted your status')
}
if (status.favoritedBy) {
2018-02-21 06:08:26 +01:00
await t.expect(getNthStatus(i).find('.status-header span').innerText)
2018-02-20 02:04:37 +01:00
.contains(status.favoritedBy + ' favorited your status')
}
// hovering forces TestCafé to scroll to that element: https://git.io/vABV2
2018-02-24 23:49:28 +01:00
if (i % SCROLL_INTERVAL === (SCROLL_INTERVAL - 1)) { // only scroll every nth element
2018-02-21 06:08:26 +01:00
await t.hover(getNthStatus(i))
.expect($('.loading-footer').exist).notOk()
2018-02-20 02:04:37 +01:00
}
}
2018-02-20 03:25:59 +01:00
}
2018-02-21 06:08:26 +01:00
2018-02-24 23:49:28 +01:00
export async function scrollToTopOfTimeline (t) {
let i = await getFirstVisibleStatus().getAttribute('aria-posinset')
while (true) {
await t.hover(getNthStatus(i))
.expect($('.loading-footer').exist).notOk()
i -= SCROLL_INTERVAL
if (i <= 0) {
break
}
}
}
2018-02-21 06:08:26 +01:00
export async function scrollToBottomOfTimeline (t) {
2018-02-24 23:49:28 +01:00
let i = 0
2018-02-21 06:08:26 +01:00
while (true) {
2018-02-24 23:49:28 +01:00
await t.hover(getNthStatus(i))
2018-02-21 06:08:26 +01:00
.expect($('.loading-footer').exist).notOk()
2018-02-24 23:49:28 +01:00
let size = await getNthStatus(i).getAttribute('aria-setsize')
i += SCROLL_INTERVAL
if (i >= size - 1) {
2018-02-21 06:08:26 +01:00
break
}
}
2018-02-21 06:30:16 +01:00
}
2018-02-21 18:26:22 +01:00
export async function scrollToStatus (t, n) {
2018-03-16 01:33:52 +01:00
for (let i = 0; i <= n; i += SCROLL_INTERVAL) {
await t.hover(getNthStatus(i))
.expect($('.loading-footer').exist).notOk()
if (i < n) {
await t.hover(getNthStatus(i).find('.status-toolbar'))
.expect($('.loading-footer').exist).notOk()
}
2018-02-21 18:26:22 +01:00
}
await t.hover(getNthStatus(n))
}
export async function clickToNotificationsAndBackHome (t) {
await t.click(notificationsNavButton)
.expect(getUrl()).eql('http://localhost:4002/notifications')
.click(homeNavButton)
.expect(getUrl()).eql('http://localhost:4002/')
}