Skip to content

Commit

Permalink
[PR feedback] Allow cy.mount to have a configurable provider props
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Jul 17, 2023
1 parent 1db5f22 commit 0fadabd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 105 deletions.
7 changes: 6 additions & 1 deletion cypress/support/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { ReactNode } from 'react';
import { mount } from 'cypress/react';
import { ContextObject, Result, RunOptions } from 'axe-core';
import { realPress } from 'cypress-real-events/commands/realPress';
import type { EuiProviderProps } from '../../src/components/provider';

type KeyOrShortcut = Parameters<typeof realPress>[0];
type RealPressOptions = Parameters<typeof realPress>[1];
Expand Down Expand Up @@ -30,7 +32,10 @@ declare global {
/**
* Mounts components with a basic `EuiProvider` wrapper
*/
mount: typeof mount;
mount: <T = {}>(
children: ReactNode,
options?: { providerProps?: Partial<EuiProviderProps<T>> }
) => ReturnType<typeof mount>;

/**
* This ensures the correct testing window has focus when using Cypress Real Events.
Expand Down
5 changes: 3 additions & 2 deletions cypress/support/setup/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React from 'react';
import { mount as cypressMount } from 'cypress/react';
import { EuiProvider } from '../../../src';

Cypress.Commands.add('mount', (children) => {
return cypressMount(<EuiProvider>{children}</EuiProvider>);
Cypress.Commands.add('mount', (children, options = {}) => {
const { providerProps } = options;
return cypressMount(<EuiProvider {...providerProps}>{children}</EuiProvider>);
});
37 changes: 13 additions & 24 deletions src/services/breakpoint/current_breakpoint.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
/// <reference types="../../../cypress/support" />

import React from 'react';
import { mount } from 'cypress/react'; // cy.mount is configured to automatically wrap <EuiProvider>, which we're already using manually here

import { EuiProvider } from '../../components/provider';
import { useCurrentEuiBreakpoint } from './';

describe('useCurrentEuiBreakpoint', () => {
Expand All @@ -29,11 +27,7 @@ describe('useCurrentEuiBreakpoint', () => {
describe('with default EUI theme breakpoints', () => {
beforeEach(() => {
cy.viewport(1600, 600);
mount(
<EuiProvider>
<MockComponent />
</EuiProvider>
);
cy.mount(<MockComponent />);
cy.wait(50); // Throttle race conditions - won't typically happen in production, but Cypress does everything extremely fast
});

Expand Down Expand Up @@ -71,23 +65,18 @@ describe('useCurrentEuiBreakpoint', () => {

describe('with custom breakpoints', () => {
beforeEach(() => {
mount(
<EuiProvider
modify={{
breakpoint: {
xxs: 0,
xs: 250,
s: 500,
m: 1000,
l: 1500,
xl: 2000,
xxl: 2500,
},
}}
>
<MockComponent />
</EuiProvider>
);
const customBreakpoints = {
xxs: 0,
xs: 250,
s: 500,
m: 1000,
l: 1500,
xl: 2000,
xxl: 2500,
};
cy.mount(<MockComponent />, {
providerProps: { modify: { breakpoint: customBreakpoints } },
});
cy.wait(50); // Throttle race conditions - won't typically happen in production, but Cypress does everything extremely fast
});

Expand Down
111 changes: 33 additions & 78 deletions src/services/breakpoint/is_within_hooks.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
/// <reference types="../../../cypress/support" />

import React, { FunctionComponent } from 'react';
import { mount } from 'cypress/react'; // mount is configured to automatically wrap <EuiProvider>, which we're already using manually here

import { EuiProvider } from '../../components/provider';
import { _EuiThemeBreakpoint } from '../../global_styling/variables/breakpoint';
import {
useIsWithinBreakpoints,
Expand All @@ -32,51 +30,34 @@ describe('useIsWithinBreakpoints', () => {

it('returns true if the current breakpoint size is in the passed sizes array', () => {
cy.viewport(300, 600);
mount(
<EuiProvider>
<MockComponent sizes={['xs', 's', 'm']} />
</EuiProvider>
);
cy.mount(<MockComponent sizes={['xs', 's', 'm']} />);
cy.get('[data-test-subj]').should('exist');
});

it('returns false if the current breakpoint size is outside the passed sizes array', () => {
cy.viewport(1400, 600);
mount(
<EuiProvider>
<MockComponent sizes={['xs', 's', 'm']} />
</EuiProvider>
);
cy.mount(<MockComponent sizes={['xs', 's', 'm']} />);
cy.get('[data-test-subj]').should('not.exist');
});

it('returns false always if isResponsive is passed as false', () => {
cy.viewport(300, 600);
mount(
<EuiProvider>
<MockComponent sizes={['xs', 's', 'm']} isResponsive={false} />
</EuiProvider>
);
cy.mount(<MockComponent sizes={['xs', 's', 'm']} isResponsive={false} />);
cy.get('[data-test-subj]').should('not.exist');
});

it('correctly handles custom breakpoint sizes', () => {
const customBreakpoints = {
xs: 0,
s: 500,
m: 1000,
l: 1500,
xl: 2000,
};
cy.viewport(1500, 600);
mount(
<EuiProvider
modify={{
breakpoint: {
xs: 0,
s: 500,
m: 1000,
l: 1500,
xl: 2000,
},
}}
>
<MockComponent sizes={['l']} />
</EuiProvider>
);
cy.mount(<MockComponent sizes={['l']} />, {
providerProps: { modify: { breakpoint: customBreakpoints } },
});
cy.get('[data-test-subj]').should('exist');
});
});
Expand All @@ -91,39 +72,26 @@ describe('useIsWithinMaxBreakpoint', () => {

it('returns true if the current breakpoint size is smaller than the passed max size', () => {
cy.viewport(300, 600);
mount(
<EuiProvider>
<MockComponent size="m" />
</EuiProvider>
);
cy.mount(<MockComponent size="m" />);
cy.get('[data-test-subj]').should('exist');
});

it('returns false if the current breakpoint size is larger than the passed max size', () => {
cy.viewport(1400, 600);
mount(
<EuiProvider>
<MockComponent size="m" />
</EuiProvider>
);
cy.mount(<MockComponent size="m" />);
cy.get('[data-test-subj]').should('not.exist');
});

it('correctly handles custom breakpoint sizes', () => {
const customBreakpoints = {
m: 1500,
l: 1800,
xl: 2000,
};
cy.viewport(1400, 600);
mount(
<EuiProvider
modify={{
breakpoint: {
m: 1500,
l: 1800,
xl: 2000,
},
}}
>
<MockComponent size="m" />
</EuiProvider>
);
cy.mount(<MockComponent size="m" />, {
providerProps: { modify: { breakpoint: customBreakpoints } },
});
cy.get('[data-test-subj]').should('exist');
});
});
Expand All @@ -138,39 +106,26 @@ describe('useIsWithinMinBreakpoint', () => {

it('returns true if the current breakpoint size is larger than the passed min size', () => {
cy.viewport(800, 600);
mount(
<EuiProvider>
<MockComponent size="m" />
</EuiProvider>
);
cy.mount(<MockComponent size="m" />);
cy.get('[data-test-subj]').should('exist');
});

it('returns false if the current breakpoint size is smaller than the passed min size', () => {
cy.viewport(600, 600);
mount(
<EuiProvider>
<MockComponent size="m" />
</EuiProvider>
);
cy.mount(<MockComponent size="m" />);
cy.get('[data-test-subj]').should('not.exist');
});

it('correctly handles custom breakpoint sizes', () => {
const customBreakpoints = {
m: 600,
l: 800,
xl: 1000,
};
cy.viewport(600, 600);
mount(
<EuiProvider
modify={{
breakpoint: {
m: 600,
l: 800,
xl: 1000,
},
}}
>
<MockComponent size="m" />
</EuiProvider>
);
cy.mount(<MockComponent size="m" />, {
providerProps: { modify: { breakpoint: customBreakpoints } },
});
cy.get('[data-test-subj]').should('exist');
});
});

0 comments on commit 0fadabd

Please sign in to comment.