-
Notifications
You must be signed in to change notification settings - Fork 226
Replaced Vuetify based copyToken with StudioCopyToken #5303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Abhishek-Punhani
wants to merge
3
commits into
learningequality:unstable
Choose a base branch
from
Abhishek-Punhani:Issue5064
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+184
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
contentcuration/contentcuration/frontend/settings/pages/Account/StudioCopyToken.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<template> | ||
|
||
<div class="token-input-wrapper"> | ||
<KCircularLoader | ||
v-if="loading" | ||
size="38" | ||
class="loader" | ||
/> | ||
<template v-else> | ||
<KTextbox | ||
ref="tokenInput" | ||
:value="displayToken" | ||
readonly | ||
class="notranslate token-input" | ||
label="Token" | ||
:floatingLabel="false" | ||
:appearanceOverrides="{ maxWidth: 'none !important', width: '100% !important' }" | ||
/> | ||
<KIconButton | ||
icon="copy" | ||
:text="$tr('copyTokenButton')" | ||
class="copy-button" | ||
@click="copyToClipboard" | ||
/> | ||
</template> | ||
</div> | ||
|
||
</template> | ||
|
||
|
||
<script> | ||
|
||
export default { | ||
name: 'StudioCopyToken', | ||
props: { | ||
token: { type: String, required: true }, | ||
loading: { type: Boolean, default: false }, | ||
hyphenate: { type: Boolean, default: true }, | ||
}, | ||
computed: { | ||
displayToken() { | ||
return this.hyphenate ? this.token.slice(0, 5) + '-' + this.token.slice(5) : this.token; | ||
}, | ||
successMessage() { | ||
return this.$tr('tokenCopied'); | ||
}, | ||
}, | ||
methods: { | ||
copyToClipboard() { | ||
if (!this.token.trim()) { | ||
this.$store.dispatch('showSnackbarSimple', this.$tr('tokenCopyFailed')); | ||
return; | ||
} | ||
if (navigator.clipboard) { | ||
navigator.clipboard | ||
.writeText(this.displayToken) | ||
.then(() => { | ||
this.$store.dispatch('showSnackbarSimple', this.successMessage); | ||
}) | ||
.catch(() => { | ||
this.$store.dispatch('showSnackbarSimple', this.$tr('tokenCopyFailed')); | ||
}); | ||
} else { | ||
this.$store.dispatch('showSnackbarSimple', this.$tr('tokenCopyFailed')); | ||
} | ||
}, | ||
}, | ||
$trs: { | ||
copyTokenButton: 'Copy token', | ||
tokenCopied: 'Token copied!', | ||
tokenCopyFailed: 'Failed to copy token.', | ||
}, | ||
}; | ||
|
||
</script> | ||
|
||
|
||
<style scoped> | ||
|
||
.token-input-wrapper { | ||
display: flex; | ||
align-items: start; | ||
justify-content: center; | ||
} | ||
|
||
.loader { | ||
margin: auto; | ||
} | ||
|
||
.token-input { | ||
flex: 1; | ||
width: 100%; | ||
max-width: none; | ||
} | ||
|
||
.copy-button { | ||
opacity: 0.8; | ||
} | ||
|
||
.copy-button:hover { | ||
opacity: 1; | ||
} | ||
|
||
</style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
contentcuration/contentcuration/frontend/settings/pages/__tests__/studioCopyToken.spec.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { mount } from '@vue/test-utils'; | ||
import StudioCopyToken from '../Account/StudioCopyToken.vue'; | ||
|
||
function makeWrapper(props = {}) { | ||
return mount(StudioCopyToken, { | ||
propsData: { | ||
token: 'testtoken', | ||
...props, | ||
}, | ||
mocks: { | ||
$store: { dispatch: jest.fn() }, | ||
$tr: key => key, | ||
}, | ||
}); | ||
} | ||
|
||
describe('StudioCopyToken', () => { | ||
it('displays hyphenated token by default', () => { | ||
const wrapper = makeWrapper(); | ||
const input = wrapper.findComponent({ ref: 'tokenInput' }); | ||
expect(input.props('value')).toBe('testt-oken'); | ||
}); | ||
|
||
it('displays token without hyphen if hyphenate is false', () => { | ||
const wrapper = makeWrapper({ hyphenate: false }); | ||
const input = wrapper.findComponent({ ref: 'tokenInput' }); | ||
expect(input.props('value')).toBe('testtoken'); | ||
}); | ||
|
||
it('shows loader when loading is true', () => { | ||
const wrapper = makeWrapper({ loading: true }); | ||
expect(wrapper.findComponent({ name: 'KCircularLoader' }).exists()).toBe(true); | ||
expect(wrapper.findComponent({ ref: 'tokenInput' }).exists()).toBe(false); | ||
}); | ||
|
||
it('should fire a copy operation on button click', async () => { | ||
const writeText = jest.fn().mockResolvedValue(); | ||
Object.assign(navigator, { | ||
clipboard: { writeText }, | ||
}); | ||
const wrapper = makeWrapper(); | ||
await wrapper.find('.copy-button').trigger('click'); | ||
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('testt-oken'); | ||
}); | ||
|
||
it('dispatches snackbar on successful copy', async () => { | ||
const writeText = jest.fn().mockResolvedValue(); | ||
Object.assign(navigator, { | ||
clipboard: { writeText }, | ||
}); | ||
const wrapper = makeWrapper(); | ||
await wrapper.find('.copy-button').trigger('click'); | ||
expect(wrapper.vm.$store.dispatch).toHaveBeenCalledWith('showSnackbarSimple', 'tokenCopied'); | ||
}); | ||
|
||
it('dispatches snackbar on failed copy', async () => { | ||
const writeText = jest.fn().mockRejectedValue(); | ||
Object.assign(navigator, { | ||
clipboard: { writeText }, | ||
}); | ||
const wrapper = makeWrapper(); | ||
await wrapper.find('.copy-button').trigger('click'); | ||
expect(wrapper.vm.$store.dispatch).toHaveBeenCalledWith( | ||
'showSnackbarSimple', | ||
'tokenCopyFailed', | ||
); | ||
}); | ||
|
||
it('dispatches snackbar if token is empty', async () => { | ||
const wrapper = makeWrapper({ token: ' ' }); | ||
await wrapper.find('.copy-button').trigger('click'); | ||
expect(wrapper.vm.$store.dispatch).toHaveBeenCalledWith( | ||
'showSnackbarSimple', | ||
'tokenCopyFailed', | ||
); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.