Skip to content

Commit cce81b7

Browse files
author
Jesse Haigh
committed
tests
1 parent c2abb95 commit cce81b7

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

src/components/ContentNode.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ function renderNode(createElement, references) {
274274
fileType: node.fileType,
275275
content: node.code,
276276
showLineNumbers: node.showLineNumbers,
277+
copyToClipboard: node.copyToClipboard ?? false,
277278
};
278279
return createElement(CodeListing, { props });
279280
}

src/components/ContentNode/CodeListing.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
</Filename>
2424
<div class="container-general" ref="scrollContainer">
2525
<button
26+
v-if="copyToClipboard"
2627
v-show="showCopyButton"
2728
class="copy-button"
2829
ref="copyButton"
2930
:class="{ copied: isCopied, visible: buttonPositioned }"
30-
@click="copyToClipboard"
31+
@click="copyCodeToClipboard"
3132
@update="handleScroll"
3233
aria-label="Copy code to clipboard"
3334
>
@@ -112,6 +113,10 @@ export default {
112113
type: Array,
113114
required: true,
114115
},
116+
copyToClipboard: {
117+
type: Boolean,
118+
required: true,
119+
},
115120
startLineNumber: {
116121
type: Number,
117122
default: () => 1,
@@ -188,7 +193,7 @@ export default {
188193
this.showCopyButton = true;
189194
}, 500);
190195
}, 100),
191-
copyToClipboard() {
196+
copyCodeToClipboard() {
192197
const lines = this.content;
193198
const text = lines.join('\n');
194199
navigator.clipboard.writeText(text)

tests/unit/components/ContentNode.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ describe('ContentNode', () => {
101101
syntax: 'swift',
102102
fileType: 'swift',
103103
code: ['foobar'],
104+
copyToClipboard: false,
104105
};
105106

106107
it('renders a `CodeListing`', () => {
@@ -111,6 +112,7 @@ describe('ContentNode', () => {
111112
expect(codeListing.props('syntax')).toBe(listing.syntax);
112113
expect(codeListing.props('fileType')).toBe(listing.fileType);
113114
expect(codeListing.props('content')).toEqual(listing.code);
115+
expect(codeListing.props('copyToClipboard')).toEqual(listing.copyToClipboard);
114116
expect(codeListing.isEmpty()).toBe(true);
115117
});
116118

@@ -138,6 +140,29 @@ describe('ContentNode', () => {
138140
});
139141
});
140142

143+
describe('with type="codeListing" and copy set', () => {
144+
const listing = {
145+
type: 'codeListing',
146+
syntax: 'swift',
147+
fileType: 'swift',
148+
code: ['foobar'],
149+
copyToClipboard: true,
150+
};
151+
152+
// renders a copy button
153+
it('renders a copy button', () => {
154+
const wrapper = mountWithItem(listing);
155+
156+
const codeListing = wrapper.find('.content').find(CodeListing);
157+
expect(codeListing.exists()).toBe(true);
158+
expect(codeListing.props('syntax')).toBe(listing.syntax);
159+
expect(codeListing.props('fileType')).toBe(listing.fileType);
160+
expect(codeListing.props('content')).toEqual(listing.code);
161+
expect(codeListing.props('copyToClipboard')).toEqual(listing.copyToClipboard);
162+
expect(codeListing.isEmpty()).toBe(true);
163+
});
164+
});
165+
141166
describe('with type="endpointExample"', () => {
142167
it('renders an `EndpointExample`', () => {
143168
const request = {

tests/unit/components/ContentNode/CodeListing.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,32 @@ describe('CodeListing', () => {
139139
expect(wrapper.html().includes('.syntax')).toBe(false);
140140
});
141141

142+
it('does not show copy button when its disabled', async () => {
143+
const wrapper = shallowMount(CodeListing, {
144+
propsData: {
145+
syntax: 'swift',
146+
content: ['let foo = "bar"'],
147+
copyToClipboard: false,
148+
},
149+
});
150+
await flushPromises();
151+
152+
expect(wrapper.find('.copy-button').exists()).toBe(false);
153+
});
154+
155+
it('shows copy button when its enabled', async () => {
156+
const wrapper = shallowMount(CodeListing, {
157+
propsData: {
158+
syntax: 'swift',
159+
content: ['let foo = "bar"'],
160+
copyToClipboard: true,
161+
},
162+
});
163+
await flushPromises();
164+
165+
expect(wrapper.find('.copy-button').exists()).toBe(true);
166+
});
167+
142168
it('renders code with empty spaces', async () => {
143169
const wrapper = shallowMount(CodeListing, {
144170
propsData: {

0 commit comments

Comments
 (0)