Skip to content

Commit

Permalink
Added Karma/Jasmine test framework for client code, added unit test, …
Browse files Browse the repository at this point in the history
…client code optimizations
  • Loading branch information
Shadowsith committed Mar 19, 2023
1 parent 668b30a commit 625cffe
Show file tree
Hide file tree
Showing 7 changed files with 7,839 additions and 498 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ pack:
tar -czvf mattermost-pr0gramm-plugin.tar.gz mattermost-pr0gramm-plugin

test:
go test ./server -v
go test ./server -v
npm test
41 changes: 21 additions & 20 deletions client/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ClientSettings,
} from './types.jsx';

class PostWillRenderEmbed extends React.Component {
export class PostWillRenderEmbed extends React.Component {
static plugin;
/**
* @type {ClientSettings}
Expand Down Expand Up @@ -219,25 +219,24 @@ class PostWillRenderEmbed extends React.Component {
const img = document.createElement('img');
img.src = url;
img.className = 'mt-1';
img.style.maxHeight = maxHeight + 'px';
img.onclick = e => {
try {
const modal = document.getElementById(this.uid + '_modal');
const modalImg = document.getElementById(this.uid + '_modal_img');
modalImg.src = e.target.src;
modal.style.zIndex = 1000;
const self = this;
document.body.appendChild(modal);
modal.style.display = 'block';
modal.onclick = () => {
const uidElem = document.getElementById(self.uid);
modal.style.display = 'none';
modal.style.zIndex = 1;
uidElem.parentElement.appendChild(modal);
}
} catch {
}
img.style.maxHeight = `${maxHeight}px`;

const showModal = () => {
const modal = document.getElementById(`${this.uid}_modal`);
const modalImg = document.getElementById(`${this.uid}_modal_img`);
modalImg.src = img.src;
modal.style.zIndex = 1000;
const uidElem = document.getElementById(this.uid);
document.body.appendChild(modal);
modal.style.display = 'block';
modal.addEventListener('click', () => {
modal.style.display = 'none';
modal.style.zIndex = 1;
uidElem.parentElement.appendChild(modal);
});
}
img.addEventListener('click', showModal);

return img;
}

Expand Down Expand Up @@ -288,4 +287,6 @@ class Pr0grammPlugin {
}
}

window.registerPlugin('pr0gramm', new Pr0grammPlugin());
if (window.registerPlugin != null) {
window.registerPlugin('pr0gramm', new Pr0grammPlugin());
}
10 changes: 10 additions & 0 deletions client/index.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PostWillRenderEmbed } from './index.jsx';

describe('PostWillRenderEmbed', () => {
it('should return a valid UUIDv4 string', () => {
const post = new PostWillRenderEmbed(null);
const uuid = post.uuidv4();
const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[8|9|aA|bB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
expect(uuid).toMatch(uuidRegex);
});
});
45 changes: 45 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', 'webpack'],
files: [
'client/*.spec.jsx'
],
preprocessors: {
'client/*.spec.jsx': ['webpack', 'sourcemap']
},
webpack: {
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react',
[
"@babel/preset-env",
{
"modules": "commonjs",
"targets": {
"node": "current"
}
}
]
],
},
},
},
{
test: /\.(css)$/,
use: ['style-loader', 'css-loader'],
},
],
}
},
reporters: ['progress'],
browsers: ['ChromeHeadless'],
singleRun: true,
})
}
Loading

0 comments on commit 625cffe

Please sign in to comment.