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: 1152df072d48ef357878fbc1afdd346c913dabf1 (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
import { nextTick } from 'vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { stubComponent } from 'helpers/stub_component';
import ObservabilityContainer from '~/observability/components/observability_container.vue';
import ObservabilitySkeleton from '~/observability/components/skeleton/index.vue';
import { buildClient } from '~/observability/client';

jest.mock('~/observability/client');

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

  const mockSkeletonOnContentLoaded = jest.fn();
  const mockSkeletonOnError = jest.fn();

  const OAUTH_URL = 'https://example.com/oauth';
  const TRACING_URL = 'https://example.com/tracing';
  const PROVISIONING_URL = 'https://example.com/provisioning';

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

    buildClient.mockReturnValue({});

    wrapper = shallowMountExtended(ObservabilityContainer, {
      propsData: {
        oauthUrl: OAUTH_URL,
        tracingUrl: TRACING_URL,
        provisioningUrl: PROVISIONING_URL,
      },
      stubs: {
        ObservabilitySkeleton: stubComponent(ObservabilitySkeleton, {
          methods: { onContentLoaded: mockSkeletonOnContentLoaded, onError: mockSkeletonOnError },
        }),
      },
      slots: {
        default: {
          render(h) {
            h(`<div>mockedComponent</div>`);
          },
          name: 'MockComponent',
          props: {
            observabilityClient: {
              type: Object,
              required: true,
            },
          },
        },
      },
    });
  });

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

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

  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 ObservabilitySkeleton', () => {
    const skeleton = wrapper.findComponent(ObservabilitySkeleton);
    expect(skeleton.exists()).toBe(true);
  });

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

  it('renders the slot content and removes the iframe on oauth success message', async () => {
    dispatchMessageEvent('success');

    await nextTick();

    expect(mockSkeletonOnContentLoaded).toHaveBeenCalledTimes(1);

    const slotComponent = findSlotComponent();
    expect(slotComponent.exists()).toBe(true);
    expect(buildClient).toHaveBeenCalledWith({
      provisioningUrl: PROVISIONING_URL,
      tracingUrl: TRACING_URL,
    });
    expect(findIframe().exists()).toBe(false);
  });

  it('does not render the slot content and removes the iframe on oauth error message', async () => {
    dispatchMessageEvent('error');

    await nextTick();

    expect(mockSkeletonOnError).toHaveBeenCalledTimes(1);

    expect(findSlotComponent().exists()).toBe(false);
    expect(findIframe().exists()).toBe(false);
    expect(buildClient).not.toHaveBeenCalled();
  });

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

    expect(mockSkeletonOnContentLoaded).toHaveBeenCalledTimes(1);
  });

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

    expect(mockSkeletonOnContentLoaded).toHaveBeenCalledTimes(0);
    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(mockSkeletonOnContentLoaded).toHaveBeenCalledTimes(0);
  });
});