aboutsummaryrefslogtreecommitdiffstats
path: root/cypress/e2e/core-utils.ts
blob: 4756836387a6f193d5c3ef751a52b3afde80631d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

/**
 * Get the unified search modal (if open)
 */
export function getUnifiedSearchModal() {
	return cy.get('#unified-search')
}

/**
 * Open the unified search modal
 */
export function openUnifiedSearch() {
	cy.get('button[aria-label="Unified search"]').click({ force: true })
	// wait for it to be open
	getUnifiedSearchModal().should('be.visible')
}

/**
 * Close the unified search modal
 */
export function closeUnifiedSearch() {
	getUnifiedSearchModal().find('button[aria-label="Close"]').click({ force: true })
	getUnifiedSearchModal().should('not.be.visible')
}

/**
 * Get the input field of the unified search
 */
export function getUnifiedSearchInput() {
	return getUnifiedSearchModal().find('[data-cy-unified-search-input]')
}

export enum UnifiedSearchFilter {
	FilterCurrentView = 'current-view',
	Places = 'places',
	People = 'people',
	Date = 'date',
}

/**
 * Get a filter action from the unified search
 * @param filter The filter to get
 */
export function getUnifiedSearchFilter(filter: UnifiedSearchFilter) {
	return getUnifiedSearchModal().find(`[data-cy-unified-search-filters] [data-cy-unified-search-filter="${CSS.escape(filter)}"]`)
}

/**
 * Assertion that an element is fully within the current viewport.
 * @param $el The element
 * @param expected If the element is expected to be fully in viewport or not fully
 * @example
 * ```js
 * cy.get('#my-element')
 *   .should(beFullyInViewport)
 * ```
 */
export function beFullyInViewport($el: JQuery<HTMLElement>, expected = true) {
	const { top, left, bottom, right } = $el.get(0)!.getBoundingClientRect()
	const innerHeight = Cypress.$('body').innerHeight()!
	const innerWidth = Cypress.$('body').innerWidth()!
	const fullyVisible = top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth

	console.debug(`fullyVisible: ${fullyVisible}, top: ${top >= 0}, left: ${left >= 0}, bottom: ${bottom <= innerHeight}, right: ${right <= innerWidth}`)

	if (expected) {
		// eslint-disable-next-line no-unused-expressions
		expect(fullyVisible, 'Fully within viewport').to.be.true
	} else {
		// eslint-disable-next-line no-unused-expressions
		expect(fullyVisible, 'Not fully within viewport').to.be.false
	}
}

/**
 * Opposite of `beFullyInViewport` - resolves when element is not or only partially in viewport.
 * @param $el The element
 * @example
 * ```js
 * cy.get('#my-element')
 *   .should(notBeFullyInViewport)
 * ```
 */
export function notBeFullyInViewport($el: JQuery<HTMLElement>) {
	return beFullyInViewport($el, false)
}