Skip to content

Commit

Permalink
Merge branch 'pu/cw/jestTests' into 'main'
Browse files Browse the repository at this point in the history
tests(Tinebase) add jest logic tests

See merge request tine20/tine20!6355
  • Loading branch information
corneliusweiss committed Dec 13, 2024
2 parents 9bc7366 + 4fff9d3 commit 7f35ced
Show file tree
Hide file tree
Showing 33 changed files with 1,021 additions and 1,747 deletions.
3 changes: 2 additions & 1 deletion tests/js/jest/Array.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const sum = require('ux/Array.js');
// grr this pollutes global array object! how to cope whith it?
require('ux/Array.js');

const testArr = ["green", "red", "blue", "red"];

Expand Down
27 changes: 27 additions & 0 deletions tests/js/jest/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# jest tests

## run tests
run all tests (using npm command)

./console src:npm 'test'

run one test file only

./console src:npm 'test -- Array.test.js'

run on test only

./console src:npm 'test -- Array.test.js --testNamePattern "Array.diff"'

## debuging tests
run tests in dbg mode

./console src:npm 'run test-dbg'

and attach a debugger, use [phpstorm](https://www.jetbrains.com/help/phpstorm/running-and-debugging-node-js.html#ws_node_debug_remote_chrome) or [chrome](chrome://inspect)

## core concepts
* pure logic tests, no DOM interaction / testing
* no globals pollution (e.g. Tine, Ext, _, ...)
* neither in tests nor in program code to be tested
* you might need to rewrite / split your code so it stick to this rule
22 changes: 22 additions & 0 deletions tests/js/jest/common.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import common from "common";


describe('common functions', () => {
describe('html2text', () => {
it('ignores empty divs', () => {
expect(common.html2text( '<blockquote class="felamimail-body-blockquote"><div>Hello,</div><div><br></div><div>...</div></blockquote>'))
.toEqual("\nHello,\n\n...")
})

it('copes with styled div tags', () => {
expect(common.html2text( '<font face="arial, tahoma, helvetica, sans-serif" style="font-size: 11px; font-family: arial, tahoma, helvetica, sans-serif;"><span style="font-size: 11px;">​<font color="#808080">Dipl.-Phys. Cornelius Weiss</font></span></font><div style="font-size: 11px; font-family: arial, tahoma, helvetica, sans-serif;"><font face="arial, tahoma, helvetica, sans-serif" color="#808080"><span style="font-size: 11px;">Team Leader Software Engineering</span></font></div>'))
.toEqual("​Dipl.-Phys. Cornelius Weiss\nTeam Leader Software Engineering")
})

it('copes with nested blocks', () => {
expect(common.html2text('<div><div><span><font><br></font></span></div></div>'))
.toEqual("\n")
})
})

})
72 changes: 72 additions & 0 deletions tests/js/jest/data/Record.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Record from "data/Record";

let RecordCls;

describe('data/Record', () => {
beforeAll(() => {
RecordCls = Record.create([
{name: 'name'},
{name: 'date', type: 'date', dateFormat: 'Y-m-d H:i:s'},
], {
appName: 'Tinebase',
modelName: 'Test',
idProperty: 'id',
titleProperty: 'name',
recordName: 'TestRecord',
recordsName: 'TestRecords'
});
})

it('creates records by constructor', () => {
expect(RecordCls).toBeInstanceOf(Function)
const record = new RecordCls({id: 'test-id'})
expect(record).toBeInstanceOf(Record)
expect(record.id).toEqual('test-id')

})

it('assigns auto ids', () => {
expect(RecordCls).toBeInstanceOf(Function)
expect(typeof new RecordCls({}).id).toBe('number')
})

it('creates records statically by setFromJson', () => {
// @TODO deal with Date prototype overwrites
const testRecord = Record.setFromJson(JSON.stringify({id: 'abc', name: 'testname'/*, date: '2024-12-11 17:50:32'*/}), RecordCls)
expect(testRecord).toBeInstanceOf(Record)
expect(testRecord.id).toEqual('abc')
})

it('can track modifications', () => {
const testRecord = Record.setFromJson(JSON.stringify({id: 'abc', name: 'testname'/*, date: '2024-12-11 17:50:32'*/}), RecordCls)
expect(testRecord.dirty).toEqual(false)
expect(testRecord.editing).toEqual(false)
// expect(testRecord.phantom).toEqual(true)
expect(testRecord.isModified('name')).toEqual(false)

testRecord.set('name', 'update')
expect(testRecord.get('name')).toEqual('update')
expect(testRecord.isModified('name')).toEqual(true)
expect(testRecord.dirty).toEqual(true)
expect(testRecord.getChanges()).toEqual({ name: 'update' })
expect(testRecord.modified).toEqual({ name: 'testname' })

const recordData = testRecord.getData()
expect(recordData.__meta.dirty).toEqual(true)
expect(recordData.__meta.modified.name).toEqual('testname')
expect(recordData.name).toEqual('update')
})

it('copes with magic customfield names', () => {
const record = new RecordCls({})
record.set('#cftest', 'value')
expect(record.get('#cftest')).toEqual('value')
expect(JSON.stringify(record.data.customfields)).toEqual(JSON.stringify({cftest: 'value'}))
})

it('generates unique UIDs in different length', () => {
expect(Record.generateUID().length).toBe(40)
expect(Record.generateUID(5).length).toBe(5)
expect(Record.generateUID()).not.toEqual(Record.generateUID())
})
});
6 changes: 4 additions & 2 deletions tests/js/jest/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const config = {
// globalTeardown: undefined,

// A set of global variables that need to be available in all test environments
// globals: {},
globals: {},

// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",
Expand Down Expand Up @@ -103,7 +103,9 @@ const config = {
}, [])),

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},
moduleNameMapper: {
"^Ext(.*)$": "<rootDir>../../library/ExtJS/src$1",
},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
Expand Down
Loading

0 comments on commit 7f35ced

Please sign in to comment.