Skip to content

Commit

Permalink
Rewrite tests for license code generation (#195)
Browse files Browse the repository at this point in the history
* Show absolute urls in the license code

* Improve tests for LicenseCode and license-uilities

* Update Store to make it testable

* Update license-utilities testing to really test generated code

* Simplify license-utilities tests

* Simplify license-utilities tests

* Simplify license-utilities tests

* Fix docs and remove -u flag for unit testing
  • Loading branch information
obulat authored Oct 29, 2020
1 parent 2bd105f commit d61ff90
Show file tree
Hide file tree
Showing 14 changed files with 769 additions and 1,189 deletions.
44 changes: 22 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@vue/eslint-config-standard": "^5.1.2",
"@vue/test-utils": "1.0.0-beta.29",
"babel-eslint": "^10.1.0",
"chromedriver": "^83.0.1",
"chromedriver": "^86.0.0",
"clipboardy": "^2.3.0",
"eslint": "^6.8.0",
"eslint-plugin-import": "^2.22.0",
Expand Down
27 changes: 16 additions & 11 deletions src/components/LicenseCode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@
v-if="workUrl && isWeb"
:href="workUrl"
rel="cc:attributionURL"
:property="workTitle ? 'dct:title' : false"
>
<span v-if="!workTitle">{{ $t('license-use.richtext.workTitle') }}</span>
<span
v-else
property="dct:title"
>
{{ workTitle }}
</span>
{{ workTitle }}
</a>
<span
v-else-if="workTitle"
Expand All @@ -32,11 +27,11 @@
</template>
<template v-slot:creator>
<a
v-if="creatorProfileUrl && isWeb"
v-if="creatorName && creatorProfileUrl && isWeb"
:href="creatorProfileUrl"
rel="cc:attributionURL"
>
<span v-html="creatorSpan" /></a>
v-html="creatorSpan"
/>
<span
v-else-if="creatorName"
v-html="creatorSpan"
Expand Down Expand Up @@ -107,13 +102,23 @@ export default {
return this.attributionDetails.creatorName
},
creatorProfileUrl() {
const { creatorProfileUrl } = this.attributionDetails
if (creatorProfileUrl && !creatorProfileUrl.startsWith('http')) {
return `http://${creatorProfileUrl}`
}
return this.attributionDetails.creatorProfileUrl
},
workTitle() {
return this.attributionDetails.workTitle
? this.attributionDetails.workTitle
: this.$t('license-use.richtext.workTitle')
},
workUrl() {
return this.attributionDetails.workUrl
const { workUrl } = this.attributionDetails
if (workUrl && !workUrl.startsWith('http')) {
return `http://${workUrl}`
}
return workUrl
},
isWeb() {
return this.attributionType === 'web'
Expand Down
12 changes: 6 additions & 6 deletions src/components/LicenseCopy.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ export default {
return this.isWeb ? 'web' : 'print'
},
htmlLicenseParagraph() {
const data = generateHTML(this.attributionDetails, this.shortName)
const { work, creator, license, paragraph } = generateHTML(this.attributionDetails, this.shortName)
const licenseCodeSpan = this.$i18n.t('license-use.richtext.full-text', {
workTitle: data.workTitle ? data.workTitle : this.$i18n.t('license-use.richtext.workTitle'),
creator: data.creator,
license: data.licenseLink,
by: data.creator ? this.$i18n.t('license-use.richtext.by') : '',
workTitle: work || this.$i18n.t('license-use.richtext.workTitle'),
creator,
license,
by: creator ? this.$i18n.t('license-use.richtext.by') : '',
'licensed-text': this.$i18n.t('license-use.richtext.licensed-text')
})
return `${data.htmlString}${licenseCodeSpan}</p>`
return `${paragraph}${licenseCodeSpan}</p>`
},
activeTab: {
get() { return this.currentTab },
Expand Down
3 changes: 2 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Vue from 'vue'
import Buefy from 'buefy'
import i18n from './i18n'
import App from './App.vue'
import store from './store'
import createStore from './store'
import './styles/vocab.scss'

// Analytics
Expand Down Expand Up @@ -61,6 +61,7 @@ if (process.env.NODE_ENV === 'production') {
})
}

const store = createStore({})
new Vue({
store,
i18n,
Expand Down
131 changes: 69 additions & 62 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,76 @@ import { defaultAttributes, CC0Attributes, attrToShort, attrToFull, licenseUrl,

Vue.use(Vuex)

export default new Vuex.Store({
state: {
currentLicenseAttributes: { ...defaultAttributes },
attributionDetails: {
creatorName: '',
creatorProfileUrl: '',
workTitle: '',
workUrl: ''
}
},
getters: {
isLicenseSelected: state => {
/**
* By default, all four license attributes are undefined
* As soon as the first attribute(BY) is selected (true/false),
* we can show the recommended license
*/
return state.currentLicenseAttributes.BY !== undefined
},
shortName: state => {
return attrToShort(state.currentLicenseAttributes)
},
fullName: state => {
return attrToFull(state.currentLicenseAttributes)
},
licenseUrl: state => (mode) => {
return licenseUrl(state.currentLicenseAttributes, mode)
},
iconsList: state => {
return licenseIconsArr(state.currentLicenseAttributes)
}
},
mutations: {
setSelected(state, { stepName, isSelected }) {
// When a Radio button is selected, currentLicenseAttribute 'selected' attribute is updated
if (['BY', 'NC', 'ND', 'SA'].indexOf(stepName) > -1) {
state.currentLicenseAttributes = {
...state.currentLicenseAttributes,
[stepName]: isSelected
}
const defaultState = {
currentLicenseAttributes: { ...defaultAttributes },
attributionDetails: {
creatorName: '',
creatorProfileUrl: '',
workTitle: '',
workUrl: ''
}
}

const createStore = (state) => {
const initialState = { ...defaultState, ...state }
return new Vuex.Store({
state: initialState,
getters: {
isLicenseSelected: state => {
/**
* By default, all four license attributes are undefined
* As soon as the first attribute(BY) is selected (true/false),
* we can show the recommended license
*/
return state.currentLicenseAttributes.BY !== undefined
},
shortName: state => {
return attrToShort(state.currentLicenseAttributes)
},
fullName: state => {
return attrToFull(state.currentLicenseAttributes)
},
licenseUrl: state => (mode) => {
return licenseUrl(state.currentLicenseAttributes, mode)
},
iconsList: state => {
return licenseIconsArr(state.currentLicenseAttributes)
}
},
updateAttributesFromShort(state, shortName) {
if (shortName.includes('CC0')) {
state.currentLicenseAttributes = { ...CC0Attributes }
} else {
state.currentLicenseAttributes.BY = true
state.currentLicenseAttributes.NC = !!shortName.includes('NC')
state.currentLicenseAttributes.ND = !!shortName.includes('ND')
state.currentLicenseAttributes.SA = !!shortName.includes('SA')
mutations: {
setSelected(state, { stepName, isSelected }) {
// When a Radio button is selected, currentLicenseAttribute 'selected' attribute is updated
if (['BY', 'NC', 'ND', 'SA'].indexOf(stepName) > -1) {
state.currentLicenseAttributes = {
...state.currentLicenseAttributes,
[stepName]: isSelected
}
}
},
updateAttributesFromShort(state, shortName) {
if (shortName.includes('CC0')) {
state.currentLicenseAttributes = { ...CC0Attributes }
} else {
state.currentLicenseAttributes.BY = true
state.currentLicenseAttributes.NC = !!shortName.includes('NC')
state.currentLicenseAttributes.ND = !!shortName.includes('ND')
state.currentLicenseAttributes.SA = !!shortName.includes('SA')
}
},
setCreatorName(state, newName) {
state.attributionDetails.creatorName = newName
},
setCreatorProfileUrl(state, newName) {
state.attributionDetails.creatorProfileUrl = newName
},
setWorkTitle(state, newName) {
state.attributionDetails.workTitle = newName
},
setWorkUrl(state, newName) {
state.attributionDetails.workUrl = newName
}
},
setCreatorName(state, newName) {
state.attributionDetails.creatorName = newName
},
setCreatorProfileUrl(state, newName) {
state.attributionDetails.creatorProfileUrl = newName
},
setWorkTitle(state, newName) {
state.attributionDetails.workTitle = newName
},
setWorkUrl(state, newName) {
state.attributionDetails.workUrl = newName
}
}
})
})
}

export default createStore
Loading

0 comments on commit d61ff90

Please sign in to comment.