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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { stubComponent } from 'helpers/stub_component';
import ObservabilityApp from '~/observability/components/observability_app.vue';
import ObservabilitySkeleton from '~/observability/components/skeleton/index.vue';
import {
MESSAGE_EVENT_TYPE,
INLINE_EMBED_DIMENSIONS,
FULL_APP_DIMENSIONS,
SKELETON_VARIANT_EMBED,
} from '~/observability/constants';
import { darkModeEnabled } from '~/lib/utils/color_utils';
jest.mock('~/lib/utils/color_utils');
describe('ObservabilityApp', () => {
let wrapper;
const $route = {
pathname: 'https://gitlab.com/gitlab-org/',
path: 'https://gitlab.com/gitlab-org/-/observability/dashboards',
query: { otherQuery: 100 },
};
const mockSkeletonOnContentLoaded = jest.fn();
const findIframe = () => wrapper.findByTestId('observability-ui-iframe');
const TEST_IFRAME_SRC = 'https://observe.gitlab.com/9970/?groupId=14485840';
const TEST_USERNAME = 'test-user';
const mountComponent = (props) => {
wrapper = shallowMountExtended(ObservabilityApp, {
propsData: {
observabilityIframeSrc: TEST_IFRAME_SRC,
...props,
},
stubs: {
ObservabilitySkeleton: stubComponent(ObservabilitySkeleton, {
methods: { onContentLoaded: mockSkeletonOnContentLoaded },
}),
},
mocks: {
$route,
},
});
};
const dispatchMessageEvent = (message) =>
window.dispatchEvent(new MessageEvent('message', message));
beforeEach(() => {
gon.current_username = TEST_USERNAME;
});
describe('iframe src', () => {
it('should render an iframe with observabilityIframeSrc, decorated with light theme and username', () => {
darkModeEnabled.mockReturnValueOnce(false);
mountComponent();
const iframe = findIframe();
expect(iframe.exists()).toBe(true);
expect(iframe.attributes('src')).toBe(
`${TEST_IFRAME_SRC}&theme=light&username=${TEST_USERNAME}`,
);
});
it('should render an iframe with observabilityIframeSrc decorated with dark theme and username', () => {
darkModeEnabled.mockReturnValueOnce(true);
mountComponent();
const iframe = findIframe();
expect(iframe.exists()).toBe(true);
expect(iframe.attributes('src')).toBe(
`${TEST_IFRAME_SRC}&theme=dark&username=${TEST_USERNAME}`,
);
});
});
describe('iframe sandbox', () => {
it('should render an iframe with sandbox attributes', () => {
mountComponent();
const iframe = findIframe();
expect(iframe.exists()).toBe(true);
expect(iframe.attributes('sandbox')).toBe('allow-same-origin allow-forms allow-scripts');
});
});
describe('iframe kiosk query param', () => {
it('when inlineEmbed, it should set the proper kiosk query parameter', () => {
mountComponent({
inlineEmbed: true,
});
const iframe = findIframe();
expect(iframe.attributes('src')).toBe(
`${TEST_IFRAME_SRC}&theme=light&username=${TEST_USERNAME}&kiosk=inline-embed`,
);
});
});
describe('iframe size', () => {
it('should set the specified size', () => {
mountComponent({
height: INLINE_EMBED_DIMENSIONS.HEIGHT,
width: INLINE_EMBED_DIMENSIONS.WIDTH,
});
const iframe = findIframe();
expect(iframe.attributes('width')).toBe(INLINE_EMBED_DIMENSIONS.WIDTH);
expect(iframe.attributes('height')).toBe(INLINE_EMBED_DIMENSIONS.HEIGHT);
});
it('should fallback to default size', () => {
mountComponent({});
const iframe = findIframe();
expect(iframe.attributes('width')).toBe(FULL_APP_DIMENSIONS.WIDTH);
expect(iframe.attributes('height')).toBe(FULL_APP_DIMENSIONS.HEIGHT);
});
});
describe('skeleton variant', () => {
it('sets the specified skeleton variant', () => {
mountComponent({ skeletonVariant: SKELETON_VARIANT_EMBED });
const props = wrapper.findComponent(ObservabilitySkeleton).props();
expect(props.variant).toBe(SKELETON_VARIANT_EMBED);
});
it('should have a default skeleton variant', () => {
mountComponent();
const props = wrapper.findComponent(ObservabilitySkeleton).props();
expect(props.variant).toBe('dashboards');
});
});
describe('on GOUI_ROUTE_UPDATE', () => {
it('should emit a route-update event', () => {
mountComponent();
const payload = { url: '/explore' };
dispatchMessageEvent({
data: { type: MESSAGE_EVENT_TYPE.GOUI_ROUTE_UPDATE, payload },
origin: 'https://observe.gitlab.com',
});
expect(wrapper.emitted('route-update')[0]).toEqual([payload]);
});
});
describe('on GOUI_LOADED', () => {
beforeEach(() => {
mountComponent();
});
it('should call onContentLoaded method', () => {
dispatchMessageEvent({
data: { type: MESSAGE_EVENT_TYPE.GOUI_LOADED },
origin: 'https://observe.gitlab.com',
});
expect(mockSkeletonOnContentLoaded).toHaveBeenCalled();
});
it('should not call onContentLoaded method if origin is different', () => {
dispatchMessageEvent({
data: { type: MESSAGE_EVENT_TYPE.GOUI_LOADED },
origin: 'https://example.com',
});
expect(mockSkeletonOnContentLoaded).not.toHaveBeenCalled();
});
it('should not call onContentLoaded method if event type is different', () => {
dispatchMessageEvent({
data: { type: 'UNKNOWN_EVENT' },
origin: 'https://observe.gitlab.com',
});
expect(mockSkeletonOnContentLoaded).not.toHaveBeenCalled();
});
});
describe('on unmount', () => {
it('should not emit any even on route update', () => {
mountComponent();
wrapper.destroy();
dispatchMessageEvent({
data: { type: MESSAGE_EVENT_TYPE.GOUI_ROUTE_UPDATE, payload: { url: '/explore' } },
origin: 'https://observe.gitlab.com',
});
expect(wrapper.emitted('route-update')).toBeUndefined();
});
});
});
|