Skip to content

Commit

Permalink
Test Clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
pivilartisant committed Oct 16, 2023
1 parent 5ffb1d9 commit b657fb4
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 26 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
193 changes: 193 additions & 0 deletions cypress/component/integration/Clipboard.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import '../../../src/global.css';
import React from 'react';
import { Clipboard } from '../../../src/components';
import { compareSnapshot } from '../../compareSnapshot';

describe('Component | Integration | Clipboard', function () {
describe('Clipboard', () => {
describe('clipboard with only raw-content', () => {
it('should render', () => {
cy.mount(<Clipboard rawContent={'raw content'} />);

cy.get('[data-testid="clipboard-field"]').should('exist');
cy.get('[data-testid="clipboard-content"]').contains('raw content');
});

it('should match snapshot', () => {
cy.mount(<Clipboard rawContent={'raw content'} />);
compareSnapshot(cy, 'clipboard-with-only-raw-content');
});
});

describe('clipboard with diplayed content', () => {
it('should render', () => {
cy.mount(
<Clipboard
rawContent={'raw content'}
displayedContent={'formatted content'}
/>,
);

cy.get('[data-testid="clipboard-field"]').should('exist');
cy.get('[data-testid="clipboard-content"]').contains(
'formatted content',
);
});

it('should match snapshot', () => {
cy.mount(
<Clipboard
rawContent={'raw content'}
displayedContent={'formatted content'}
/>,
);
compareSnapshot(cy, 'clipboard-with-displayed-content');
});
});

describe('clipboard success flow', () => {
it('should render', () => {
cy.mount(
<Clipboard
rawContent={'raw content'}
success="Copied!"
displayedContent={'formatted content'}
/>,
);

cy.get('[data-testid="clipboard-field"]').should('exist');
cy.get('[data-testid="clipboard-content"]').contains(
'formatted content',
);
});

it('should fire the copy clipboard function', () => {
cy.mount(
<Clipboard
rawContent={'raw content'}
success="Copied!"
displayedContent={'formatted content'}
/>,
);
cy.get('[data-testid="clipboard-field"]').should('exist');
cy.get('[data-testid="clipboard-content"]').contains(
'formatted content',
);
cy.on('window:confirm', () => true);

cy.window().then((win) => {
cy.stub(win, 'prompt')
.returns(win.prompt)
.as('copyToClipboardPrompt');
});

cy.get('[data-testid="clipboard-field"]').click();
cy.get('@copyToClipboardPrompt').should('be.called');

cy.get('@copyToClipboardPrompt')
.should((prompt) => {
expect(prompt.args[0][1]).to.equal('raw content');
})
.then(() => {
cy.get('[data-testid="clipboard-input-message"]')
.should('exist')
.contains('Copied!');
});

cy.get('[data-testid="clipboard-input-message"]').should('not.exist');
});

it('should match snapshot', () => {
cy.mount(
<Clipboard
rawContent={'raw content'}
success="Copied!"
displayedContent={'formatted content'}
/>,
);
compareSnapshot(cy, 'clipboard-success-flow');
});
});

describe('clipboard raw content success flow', () => {
it('should render', () => {
cy.mount(<Clipboard rawContent={'raw content'} success="Copied!" />);

cy.get('[data-testid="clipboard-field"]').should('exist');
cy.get('[data-testid="clipboard-content"]').contains('raw content');
});

it('should fire the copy clipboard function', () => {
cy.mount(<Clipboard rawContent={'raw content'} success="Copied!" />);
cy.get('[data-testid="clipboard-field"]').should('exist');
cy.get('[data-testid="clipboard-content"]').contains('raw content');
cy.on('window:confirm', () => true);

cy.window().then((win) => {
cy.stub(win, 'prompt')
.returns(win.prompt)
.as('copyToClipboardPrompt');
});

cy.get('[data-testid="clipboard-field"]').click();
cy.get('@copyToClipboardPrompt').should('be.called');

cy.get('@copyToClipboardPrompt')
.should((prompt) => {
expect(prompt.args[0][1]).to.equal('raw content');
})
.then(() => {
cy.get('[data-testid="clipboard-input-message"]')
.should('exist')
.contains('Copied!');
});

cy.get('[data-testid="clipboard-input-message"]').should('not.exist');
});

it('should match snapshot', () => {
cy.mount(<Clipboard rawContent={'raw content'} success="Copied!" />);
compareSnapshot(cy, 'clipboard-raw-content-success-flow');
});
});

describe('clipboard error flow', () => {
it('should render', () => {
cy.mount(<Clipboard rawContent={''} error="Oupps!" />);

cy.get('[data-testid="clipboard-field"]').should('exist');
cy.get('[data-testid="clipboard-content"]')
.contains('foo')
.should('not.exist');
});

it('should not fire the copy clipboard function', () => {
cy.mount(<Clipboard rawContent={''} error="Oupps!" />);
cy.get('[data-testid="clipboard-field"]').should('exist');
cy.get('[data-testid="clipboard-content"]')
.contains('foo')
.should('not.exist');
cy.on('window:confirm', () => true);

cy.window().then((win) => {
cy.stub(win, 'prompt')
.returns(win.prompt)
.as('copyToClipboardPrompt');
});

cy.get('[data-testid="clipboard-copy-icon"]').click();
cy.get('@copyToClipboardPrompt')
.should('not.be.called')
.then(() => {
cy.get('[data-testid="clipboard-input-message"]')
.should('exist')
.contains('Oupps!');
});

cy.get('[data-testid="clipboard-input-message"]').should('not.exist');
});

// We are not testing snapshots here because we know it will fail due to the absence of raw content
});
});
});
9 changes: 9 additions & 0 deletions src/components/Clipboard/Clipboard.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ export const _Clipboard = {
/>
),
};

export const _Rawcontent = {
render: () => (
<Clipboard
rawContent="this is the content"
displayedContent="formatted content"
/>
),
};
20 changes: 0 additions & 20 deletions src/components/Clipboard/Clipboard.test.tsx

This file was deleted.

25 changes: 19 additions & 6 deletions src/components/Clipboard/Clipboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export function Clipboard(props: ClipboardProps) {

if (!displayedContent || !rawContent) {
setError(true);

setTimeout(() => {
setError(false);
}, 3000);

return;
}

Expand All @@ -58,20 +63,28 @@ export function Clipboard(props: ClipboardProps) {
onClick={handleCopyToClipboard}
{...rest}
>
<p className="text-f-primary pr-2 w-full truncate">
<p
className="text-f-primary pr-2 w-full truncate"
data-testid="clipboard-content"
>
{displayedContent}
</p>
{success ? (
<FiCheckCircle className="w-6 h-6 text-s-success" />
) : (
<FiCopy className="w-6 h-6 text-f-primary" />
<FiCopy
data-testid="clipboard-copy-icon"
className="w-6 h-6 text-f-primary"
/>
)}
</div>
{error || success ? (
<InputMessage
error={error ? errorMessage : ''}
success={success ? successMessage : ''}
/>
<div data-testid="clipboard-input-message">
<InputMessage
error={error ? errorMessage : ''}
success={success ? successMessage : ''}
/>
</div>
) : null}
</>
);
Expand Down

0 comments on commit b657fb4

Please sign in to comment.