Welcome to mirror list, hosted at ThFree Co, Russian Federation.

observability_container_spec.js « observability « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 41906b2f45d1b2617e254e8e396721c309710fa5 (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
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
import { nextTick } from 'vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import ObservabilityContainer from '~/observability/components/observability_container.vue';
import ObservabilityLoader from '~/observability/components/loader/index.vue';
import { CONTENT_STATE } from '~/observability/components/loader/constants';
import { buildClient } from '~/observability/client';
import * as Sentry from '~/sentry/sentry_browser_wrapper';
import { logError } from '~/lib/logger';

jest.mock('~/observability/client');
jest.mock('~/sentry/sentry_browser_wrapper');
jest.mock('~/lib/logger');

describe('ObservabilityContainer', () => {
  let wrapper;

  const OAUTH_URL = 'https://example.com/oauth';
  const TRACING_URL = 'https://example.com/tracing';
  const PROVISIONING_URL = 'https://example.com/provisioning';
  const SERVICES_URL = 'https://example.com/services';
  const OPERATIONS_URL = 'https://example.com/operations';
  const METRICS_URL = 'https://example.com/metrics';

  const mockClient = { mock: 'client' };

  beforeEach(() => {
    jest.spyOn(console, 'error').mockImplementation();

    buildClient.mockReturnValue(mockClient);

    wrapper = shallowMountExtended(ObservabilityContainer, {
      propsData: {
        apiConfig: {
          oauthUrl: OAUTH_URL,
          tracingUrl: TRACING_URL,
          provisioningUrl: PROVISIONING_URL,
          servicesUrl: SERVICES_URL,
          operationsUrl: OPERATIONS_URL,
          metricssUrl: METRICS_URL,
        },
      },
      slots: {
        default: {
          render(h) {
            h(`<div>mockedComponent</div>`);
          },
          name: 'MockComponent',
        },
      },
    });
  });

  const dispatchMessageEvent = (status, origin) =>
    window.dispatchEvent(
      new MessageEvent('message', {
        data: {
          type: 'AUTH_COMPLETION',
          status,
          message: 'test-message',
          statusCode: 'test-code',
        },
        origin: origin ?? new URL(OAUTH_URL).origin,
      }),
    );

  const findIframe = () => wrapper.findByTestId('observability-oauth-iframe');
  const findSlotComponent = () => wrapper.findComponent({ name: 'MockComponent' });
  const findLoader = () => wrapper.findComponent(ObservabilityLoader);

  it('should render the oauth iframe', () => {
    const iframe = findIframe();
    expect(iframe.exists()).toBe(true);
    expect(iframe.attributes('hidden')).toBe('hidden');
    expect(iframe.attributes('src')).toBe(OAUTH_URL);
    expect(iframe.attributes('sandbox')).toBe('allow-same-origin allow-forms allow-scripts');
  });

  it('should render the ObservabilityLoader', () => {
    expect(findLoader().exists()).toBe(true);
  });

  it('should not render the default slot', () => {
    expect(findSlotComponent().exists()).toBe(false);
  });

  it('should not emit observability-client-ready', () => {
    expect(wrapper.emitted('observability-client-ready')).toBeUndefined();
  });

  describe('on oauth success message', () => {
    beforeEach(async () => {
      dispatchMessageEvent('success');

      await nextTick();
    });

    it('sets the loader contentState to LOADED', () => {
      expect(findLoader().props('contentState')).toBe(CONTENT_STATE.LOADED);
    });

    it('renders the slot content', () => {
      const slotComponent = findSlotComponent();
      expect(slotComponent.exists()).toBe(true);
    });

    it('build the observability client', () => {
      expect(buildClient).toHaveBeenCalledWith(wrapper.props('apiConfig'));
    });

    it('emits observability-client-ready', () => {
      expect(wrapper.emitted('observability-client-ready')).toEqual([[mockClient]]);
    });
  });

  describe('on oauth error message', () => {
    beforeEach(async () => {
      dispatchMessageEvent('error');

      await nextTick();
    });

    it('set the loader contentState to ERROR', () => {
      expect(findLoader().props('contentState')).toBe(CONTENT_STATE.ERROR);
    });

    it('does not renders the slot content', () => {
      expect(findSlotComponent().exists()).toBe(false);
    });

    it('does not build the observability client', () => {
      expect(buildClient).not.toHaveBeenCalled();
    });

    it('does not emit observability-client-ready', () => {
      expect(wrapper.emitted('observability-client-ready')).toBeUndefined();
    });

    it('reports the error', () => {
      const e = new Error('GOB auth failed with error: test-message - status: test-code');
      expect(Sentry.captureException).toHaveBeenCalledWith(e);
      expect(logError).toHaveBeenCalledWith(e);
    });
  });

  it('handles oauth message only once', async () => {
    dispatchMessageEvent('success');
    dispatchMessageEvent('error');

    await nextTick();

    expect(buildClient).toHaveBeenCalledTimes(1);
    expect(findLoader().props('contentState')).toBe(CONTENT_STATE.LOADED);
  });

  it('only handles messages from the oauth url', () => {
    dispatchMessageEvent('success', 'www.fake-url.com');

    expect(findLoader().props('contentState')).toBe(null);
    expect(findSlotComponent().exists()).toBe(false);
    expect(findIframe().exists()).toBe(true);
  });

  it('does not handle messages if the component has been destroyed', () => {
    wrapper.destroy();

    dispatchMessageEvent('success');

    expect(findLoader().props('contentState')).toBe(null);
  });
});