1
1
/**********************************************************************
2
- * Copyright (C) 2024 Red Hat, Inc.
2
+ * Copyright (C) 2024-2025 Red Hat, Inc.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import '@testing-library/jest-dom/vitest' ;
20
20
21
21
import { render , screen } from '@testing-library/svelte' ;
22
- import { tick } from 'svelte' ;
22
+ import { createRawSnippet , tick } from 'svelte' ;
23
23
import { expect , test } from 'vitest' ;
24
24
25
25
import Tooltip from './Tooltip.svelte' ;
26
26
import { tooltipHidden } from './tooltip-store' ;
27
27
28
- test ( 'tooltip is not empty string when tooltipHidden value false' , async ( ) => {
28
+ const tip = 'test' ;
29
+
30
+ test ( 'Expect basic prop styling' , async ( ) => {
31
+ render ( Tooltip , { tip : tip } ) ;
32
+
33
+ const element = screen . getByLabelText ( 'tooltip' ) ;
34
+ expect ( element ) . toBeInTheDocument ( ) ;
35
+ expect ( element ) . toHaveClass ( 'bg-[var(--pd-tooltip-bg)]' ) ;
36
+ expect ( element ) . toHaveClass ( 'text-[var(--pd-tooltip-text)]' ) ;
37
+ expect ( element ) . toHaveClass ( 'border-[var(--pd-tooltip-border)]' ) ;
38
+ expect ( element ) . toHaveClass ( 'border-[1px]' ) ;
39
+ } ) ;
40
+
41
+ test ( 'Expect basic slot styling' , async ( ) => {
42
+ render ( Tooltip , {
43
+ tip : createRawSnippet ( ( ) => {
44
+ return {
45
+ render : ( ) : string => 'test' ,
46
+ } ;
47
+ } ) ,
48
+ } ) ;
49
+
50
+ const element = screen . getByLabelText ( 'tooltip' ) ;
51
+ expect ( element ) . toBeInTheDocument ( ) ;
52
+ expect ( element ) . toHaveClass ( 'bg-[var(--pd-tooltip-bg)]' ) ;
53
+ expect ( element ) . toHaveClass ( 'text-[var(--pd-tooltip-text)]' ) ;
54
+ expect ( element ) . toHaveClass ( 'border-[var(--pd-tooltip-border)]' ) ;
55
+ expect ( element ) . toHaveClass ( 'border-[1px]' ) ;
56
+ } ) ;
57
+
58
+ test ( 'Expect tooltip is not empty string when tooltipHidden value false' , async ( ) => {
29
59
tooltipHidden . set ( false ) ;
30
60
31
61
render ( Tooltip , { tip : 'test 1' } ) ;
@@ -40,10 +70,49 @@ test('tooltip is not empty string when tooltipHidden value false', async () => {
40
70
expect ( screen . queryByText ( 'test 1' ) ) . toBeInTheDocument ( ) ;
41
71
} ) ;
42
72
43
- test ( 'tooltip z order' , async ( ) => {
73
+ test ( 'Expect tooltip z order' , async ( ) => {
44
74
render ( Tooltip , { tip : 'my tooltip' } ) ;
45
75
46
76
// get the tooltip
47
77
const tooltip = screen . getByText ( 'my tooltip' ) ;
48
78
expect ( tooltip . parentElement ) . toHaveClass ( 'z-60' ) ;
49
79
} ) ;
80
+
81
+ test ( 'Expect class styling to apply to tip' , async ( ) => {
82
+ render ( Tooltip , { class : 'my-[5px] mx-[10px]' , tip } ) ;
83
+
84
+ const slotElement = screen . getByLabelText ( 'tooltip' ) ;
85
+ expect ( slotElement ) . toHaveClass ( 'my-[5px] mx-[10px]' ) ;
86
+ } ) ;
87
+
88
+ test ( 'Expect class styling to apply to tip snippet' , async ( ) => {
89
+ render ( Tooltip , {
90
+ class : 'my-[5px] mx-[10px]' ,
91
+ tip : createRawSnippet ( ( ) => {
92
+ return {
93
+ render : ( ) : string => 'test' ,
94
+ } ;
95
+ } ) ,
96
+ } ) ;
97
+
98
+ const slotElement = screen . getByLabelText ( 'tooltip' ) ;
99
+ expect ( slotElement ) . toHaveClass ( 'my-[5px] mx-[10px]' ) ;
100
+ } ) ;
101
+
102
+ function createTest ( props : Record < string , boolean > , locationName : string , expectedStyle = locationName ) : void {
103
+ test ( `Expect property ${ locationName } to add ${ expectedStyle } class to parent element` , ( ) => {
104
+ render ( Tooltip , { tip, ...props } ) ;
105
+ const element = screen . getByLabelText ( 'tooltip' ) ;
106
+ expect ( element ) . toBeInTheDocument ( ) ;
107
+ expect ( element . parentElement ) . toHaveClass ( expectedStyle ) ;
108
+ } ) ;
109
+ }
110
+
111
+ createTest ( { left : true } , 'left' ) ;
112
+ createTest ( { right : true } , 'right' ) ;
113
+ createTest ( { bottom : true } , 'bottom' ) ;
114
+ createTest ( { top : true } , 'top' ) ;
115
+ createTest ( { topLeft : true } , 'topLeft' , 'top-left' ) ;
116
+ createTest ( { topRight : true } , 'topRight' , 'top-right' ) ;
117
+ createTest ( { bottomLeft : true } , 'bottomLeft' , 'bottom-left' ) ;
118
+ createTest ( { bottomRight : true } , 'bottomRight' , 'bottom-right' ) ;
0 commit comments