Skip to content

feat: add unit tests for the framework #35

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
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/coverage-monitor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Coverage-Monitor
on: [pull_request]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1

- name: Install npm
run: npm install

- name: Test
run: npx jest --coverage

- name: Monitor coverage
uses: slavcodev/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
clover_file: "./coverage/clover.xml"
threshold_alert: 70
threshold_warning: 80
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ["@babel/preset-env"]
}
27 changes: 27 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {

moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
'^~/(.*)$': '<rootDir>/src/$1',
'^vue$': 'vue/dist/vue.common.js'
},
moduleFileExtensions: [
// 'ts',
'js',
'vue',
'json'
],
testEnvironment: 'jsdom',
setupFiles: ["jest-canvas-mock"],
transform: {
// '^.+\\.ts$': 'ts-jest',
'^.+\\.js$': 'babel-jest',
'.*\\.(vue)$': 'vue-jest',
},
transformIgnorePatterns: ['/node_modules/(?!p5)'],
collectCoverage: false,
collectCoverageFrom: [
'<rootDir>/src/**/*.vue',
// '<rootDir>/pages/**/*.vue'
]
}
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/vue-p5.js",
"scripts": {
"build": "rollup --config",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest --watch"
},
"repository": {
"type": "git",
Expand All @@ -22,12 +22,19 @@
},
"homepage": "https://github.com/Kinrany/vue-p5#readme",
"devDependencies": {
"@babel/core": "^7.15.8",
"@babel/preset-env": "^7.15.8",
"@rollup/plugin-commonjs": "^11.0.2",
"@rollup/plugin-node-resolve": "^7.1.1",
"@vue/test-utils": "^1.2.2",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^27.2.4",
"jest-canvas-mock": "^2.3.1",
"postcss": "^7.0.27",
"rollup": "^1.32.1",
"rollup-plugin-vue": "^5.1.6",
"vue": "^2.6.11",
"vue-jest": "^3.0.7",
"vue-template-compiler": "^2.6.11"
},
"peerDependencies": {
Expand Down
46 changes: 43 additions & 3 deletions src/p5.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ const initialEvents = [
"windowResized"
];

const initialConstants = [
'HALF_PI',
'PI',
'QUARTER_PI',
'TAU',
'TWO_PI',
'DEGREES',
'RADIANS'
]

export default {
// re-export p5 for use with other libraries
p5,
Expand All @@ -51,21 +61,51 @@ export default {
: initialEvents;
}
},
mounted() {
new p5(sketch => {
methods: {
/**
* @function extractConstants
* @param {object} sketch => sketch object
* @returns {object} of constants
*/
extractConstants: function (sketch) {
Copy link
Author

@sniperadmin sniperadmin Oct 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one works!
However, I think this approach needs to be checked as I may think it could be done better than this.

  • I thought to first generate the { key, value } set of the p5 constants first
  • Then, injecting them into the sketch just before emitting the sketch context

The cons of this approach:

  • I cannot test the method directly from the main component => gives undefined when I try to get the value in line 73
const savedKey = sketch[p5ConstName]

Workarounds

In the test file, I tested the component as if I am reusing it (re-usability). It returns the constants needed

let constantsToExport = {}
for (const p5ConstName of initialConstants) {
const savedKey = sketch[p5ConstName]

if (savedKey) {
constantsToExport[p5ConstName] = savedKey;
}
}
return { ...constantsToExport };
},

extractEvents(sketch) {
/**
* @function extractConstants
* @see line87 for more details
*/
const constants = this.extractConstants(sketch)
// NOTE: resetting sketch payload
sketch = { ...sketch, ...constants };
// emmiting with the new defined consts
this.$emit("sketch", sketch);

for (const p5EventName of this.p5Events) {
const vueEventName = p5EventName.toLowerCase();
const savedCallback = sketch[p5EventName];

sketch[p5EventName] = (...args) => {
if (savedCallback) {
savedCallback(sketch, ...args);
}
this.$emit(vueEventName, sketch, ...args);
};
}
}
},
mounted() {
new p5(sketch => {
this.extractEvents(sketch)
}, this.$el);
}
};
Expand Down
107 changes: 107 additions & 0 deletions tests/unit/p5.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import {
mount,
createLocalVue
} from '@vue/test-utils'
import p5 from '@/p5.vue'

let wrapper;

describe('p5', () => {
beforeEach(() => {
let localVue = createLocalVue()
wrapper = mount(p5, {
localVue,
})
})

it('should mount', () => {
expect(wrapper.vm).toBeTruthy()
})


it('should return exact computed events', () => {
// console.log(wrapper.vm.p5Events)
expect(wrapper.vm.p5Events).toStrictEqual([
'preload', 'setup',
'draw', 'deviceMoved',
'deviceTurned', 'deviceShaken',
'keyPressed', 'keyReleased',
'keyTyped', 'mouseMoved',
'mouseDragged', 'mousePressed',
'mouseReleased', 'mouseClicked',
'doubleClicked', 'mouseWheel',
'touchStarted', 'touchMoved',
'touchEnded', 'windowResized'
])
})


it('should return P5 constants', () => {
/**
* HALF_PI PI QUARTER_PI TAU TWO_PI DEGREES RADIANS
*/
const mockComponent = {
template: `
<p5 @sketch="sketch"></p5>
`,
data() {
return {
skObj: null
}
},
methods: {
sketch(sk) {
this.skObj = sk
}
}
}

const mockWrapper = mount(mockComponent, {
components: {p5}
})

const data = mockWrapper.vm.skObj

expect(data.HALF_PI).toStrictEqual(1.5707963267948966)
expect(data.PI).toStrictEqual(3.141592653589793)
expect(data.QUARTER_PI).toStrictEqual(0.7853981633974483)
expect(data.TAU).toStrictEqual(6.283185307179586)
expect(data.TWO_PI).toStrictEqual(6.283185307179586)
})

xit('should return rest of structure events', () => {
/**
*
preload() setup()
draw() remove()
disableFriendlyErrors
noLoop() loop()
isLooping() push()
pop() redraw() p5()
*/
});


xit('should return all P5 classes', () => {
/**
* p5.Graphics
* p5.MediaElement
* p5.File
* p5.Color
* p5.Geometry
* p5.Element
* p5.TypedDict
* p5.NumberDict
* p5.Image
* p5.Table
* p5.TableRow
* p5.PrintWriter
* p5.XML
* p5.Vector
* p5.Font
* p5.Shader
* p5.Camera
*/
});

})