Refactoring: Migrated JS core tests to promise bases approach.

This commit is contained in:
Rolf Schmidt 2020-09-11 16:45:49 +02:00 committed by Thorsten Eckel
parent f80725ed62
commit ae642683f5
2 changed files with 563 additions and 583 deletions

View file

@ -1,262 +1,246 @@
window.onload = function() { test( "ajax get 200", function(assert) {
var done = assert.async(1)
callback = function() { new Promise( (resolve, reject) => {
// ajax
App.Ajax.request({ App.Ajax.request({
type: 'GET', type: 'GET',
url: '/assets/tests/ajax-test.json', url: '/assets/tests/ajax-test.json',
success: function (data) { success: resolve,
test( "ajax get 200", function() { error: reject
});
}).then( function(data) {
ok( true, "File found!") ok( true, "File found!")
equal(data.success, true, "content parsable and ok!") equal(data.success, true, "content parsable and ok!")
equal(data.success2, undefined, "content parsable and ok!") equal(data.success2, undefined, "content parsable and ok!")
}); }, function(data) {
},
error: function (data) {
test( "ajax", function() {
ok( false, "Failed!") ok( false, "Failed!")
}); })
} .finally(done)
}); });
// ajax queueing test( "ajax - queue - ajax get 200 1/2", function(assert) {
var done = assert.async(1)
new Promise( (resolve, reject) => {
App.Ajax.request({ App.Ajax.request({
type: 'GET', type: 'GET',
url: '/tests/wait/2', url: '/tests/wait/2',
queue: true, queue: true,
success: function (data) { success: resolve,
test( "ajax - queue - ajax get 200 1/2", function() { error: reject
});
// check queue }).then( function(data) {
ok( !window.testAjax, 'ajax - queue - check queue') ok( !window.testAjax, 'ajax - queue - check queue')
window.testAjax = true; window.testAjax = true;
equal(data.success, true, "ajax - queue - content parsable and ok!") equal(data.success, true, "ajax - queue - content parsable and ok!")
equal(data.success2, undefined, "ajax - queue - content parsable and ok!") equal(data.success2, undefined, "ajax - queue - content parsable and ok!")
}); }, function(data) {
},
error: function (data) {
test( "ajax", function() {
ok( false, "Failed!") ok( false, "Failed!")
}); })
} .finally(done)
}); });
test( "ajax - queue - ajax get 200 2/2", function(assert) {
var done = assert.async(1)
new Promise( (resolve, reject) => {
App.Ajax.request({ App.Ajax.request({
type: 'GET', type: 'GET',
url: '/tests/wait/1', url: '/tests/wait/1',
queue: true, queue: true,
success: function (data) { success: resolve,
test( "ajax - queue - ajax get 200 2/2", function() { error: reject
});
}).then( function(data) {
// check queue // check queue
ok( window.testAjax, 'ajax - queue - check queue') ok( window.testAjax, 'ajax - queue - check queue')
window.testAjax = undefined; window.testAjax = undefined;
equal(data.success, true, "content parsable and ok!") equal(data.success, true, "content parsable and ok!")
equal(data.success2, undefined, "content parsable and ok!") equal(data.success2, undefined, "content parsable and ok!")
}); }, function(data) {
},
error: function (data) {
test( "ajax", function() {
ok( false, "Failed!") ok( false, "Failed!")
}); })
} .finally(done)
}); });
// ajax parallel test( "ajax - parallel - ajax get 200", function(assert) {
var done = assert.async(1)
new Promise( (resolve, reject) => {
App.Ajax.request({ App.Ajax.request({
type: 'GET', type: 'GET',
url: '/tests/wait/3', url: '/tests/wait/3',
success: function (data) { success: resolve,
test( "ajax - parallel - ajax get 200 1/2", function() { error: reject
});
// check queue new Promise( (resolve, reject) => {
ok( window.testAjaxQ, 'ajax - parallel - check queue')
window.testAjaxQ = undefined;
equal(data.success, true, "ajax - parallel - content parsable and ok!")
equal(data.success2, undefined, "ajax - parallel - content parsable and ok!")
});
},
error: function (data) {
test( "ajax", function() {
ok( false, "Failed!")
});
}
});
App.Ajax.request({ App.Ajax.request({
type: 'GET', type: 'GET',
url: '/tests/wait/1', url: '/tests/wait/1',
success: function (data) { success: resolve,
test( "ajax - parallel - ajax get 200 2/2", function() { error: reject
});
}).then( function(data) {
// check queue // check queue
ok( !window.testAjaxQ, 'ajax - parallel - check queue') ok( !window.testAjaxQ, 'ajax - parallel - check queue')
window.testAjaxQ = true; window.testAjaxQ = true;
equal(data.success, true, "content parsable and ok!") equal(data.success, true, "content parsable and ok!")
equal(data.success2, undefined, "content parsable and ok!") equal(data.success2, undefined, "content parsable and ok!")
}); }, function(data) {
},
error: function (data) {
test( "ajax", function() {
ok( false, "Failed!") ok( false, "Failed!")
}); })
} .finally(done)
}); }).then( function(data) {
// check queue
ok( window.testAjaxQ, 'ajax - parallel - check queue')
window.testAjaxQ = undefined;
equal(data.success, true, "ajax - parallel - content parsable and ok!")
equal(data.success2, undefined, "ajax - parallel - content parsable and ok!")
}, function(data) {
ok( false, "Failed!")
})
});
test('delay - test', function(assert) {
var done = assert.async(1)
// delay
window.testDelay1 = false window.testDelay1 = false
App.Delay.set(function() { new Promise( (resolve, reject) => {
test('delay - test 1 - 1/3 - should not be executed, will be reset by next set()', function() { App.Delay.set(resolve, 1000, 'delay-test1', 'level');
// check new Promise( (resolve, reject) => {
ok(false, 'delay - test 1 - 1/3 - should not be executed, will be reset by next set()') App.Delay.set(resolve, 2000, 'delay-test1', 'level');
window.testDelay1 = true; }).then( function() {
}); ok(!window.testDelay1, 'delay - 1/2')
},
1000,
'delay-test1',
'level'
);
App.Delay.set(function() {
test('delay - test 1 - 2/3', function() {
// check
ok(!window.testDelay1, 'delay - test 1 - 2/3')
window.testDelay1 = 1; window.testDelay1 = 1;
}); })
},
2000,
'delay-test1',
'level'
);
App.Delay.set(function() {
test('delay - test 1 - 2/3', function() {
// check new Promise( (resolve, reject) => {
ok(window.testDelay1, 'delay - test 1 - 2/3') App.Delay.set(resolve, 3000, 'delay-test1-verify', 'level');
}).then( function() {
ok(window.testDelay1, 'delay - 2/2')
window.testDelay1 = false; window.testDelay1 = false;
}); })
}, .finally(done)
3000, }).then( function() {
'delay-test1-verify', ok(false, 'delay - 1/2 - FAILED - should not be executed, will be reset by next set()')
'level' window.testDelay1 = true;
); })
});
App.Delay.set(function() { test('delay - test 2', function(assert) {
test('delay - test 2 - 1/3', function() { var done = assert.async(1)
// check new Promise( (resolve, reject) => {
App.Delay.set(resolve, 2000);
new Promise( (resolve, reject) => {
App.Delay.set(resolve, 1000);
}).then( function() {
ok(!window.testDelay2, 'delay - test 2 - 1/3') ok(!window.testDelay2, 'delay - test 2 - 1/3')
window.testDelay2 = 1; })
});
},
2000
);
App.Delay.set(function() {
test('delay - test 2 - 2/3', function() {
// check new Promise( (resolve, reject) => {
ok(!window.testDelay2, 'delay - test 2 - 2/3') App.Delay.set(resolve, 3000);
}); }).then( function() {
},
1000
);
App.Delay.set(function() {
test('delay - test 2 - 3/3', function() {
// check
ok(window.testDelay2, 'delay - test 2 - 3/3') ok(window.testDelay2, 'delay - test 2 - 3/3')
}); })
}, .finally(done)
3000 }).then( function() {
); ok(!window.testDelay2, 'delay - test 2 - 2/3')
window.testDelay2 = 1;
})
});
window.testDelay3 = 1; test('delay - test 3', function(assert) {
App.Delay.set(function() { var done = assert.async(1)
test('delay - test 3 - 1/1', function() {
// check new Promise( (resolve, reject) => {
ok(false, 'delay - test 3 - 1/1') App.Delay.set(resolve, 1000, 'delay3');
});
},
1000,
'delay3'
);
App.Delay.clear('delay3') App.Delay.clear('delay3')
ok(true, 'delay - test 3 - 1/1')
done()
}).then( function() {
ok(false, 'delay - test 3 - 1/1 - FAILED')
})
});
App.Delay.set(function() { test('delay - test 4', function(assert) {
test('delay - test 4 - 1/1', function() { var done = assert.async(1)
// check new Promise( (resolve, reject) => {
ok(false, 'delay - test 4 - 1/1') App.Delay.set(resolve, 1000, undefined, 'Page');
});
},
1000,
undefined,
'Page'
);
App.Delay.clearLevel('Page') App.Delay.clearLevel('Page')
ok(true, 'delay - test 4 - 1/1')
done()
}).then( function() {
ok(false, 'delay - test 4 - 1/1 - FAILED')
})
});
test('interval - test 1', function(assert) {
var done = assert.async(1)
// interval 1
window.testInterval1 = 1 window.testInterval1 = 1
App.Interval.set(function() { App.Interval.set(function() {
window.testInterval1 += 1; window.testInterval1 += 1;
}, },
2000, 100,
'interval-test1' 'interval-test1'
); );
App.Delay.set(function() {
test('interval - test 1 - 1/2', function() {
// check new Promise( (resolve, reject) => {
equal(window.testInterval1, 4, 'interval - test 1') App.Delay.set(resolve, 1000);
new Promise( (resolve, reject) => {
App.Delay.set(resolve, 2000);
}).then( function() {
equal(window.testInterval1, window.testInterval1Backup, 'interval - did not change after clear interval')
})
.finally(done)
}).then( function() {
notEqual(window.testInterval1, 1, 'interval - interval moved up')
App.Interval.clear('interval-test1') App.Interval.clear('interval-test1')
}); window.testInterval1Backup = window.testInterval1;
}, })
5200 })
);
App.Delay.set(function() {
test('interval - test 1 - 2/2', function() {
// check test('interval - test 2', function(assert) {
equal(window.testInterval1, 4, 'interval - test after clear') var done = assert.async(1)
});
},
6500
);
window.testInterval1 = 1
// interval 2
window.testInterval2 = 1
App.Interval.set(function() { App.Interval.set(function() {
window.testInterval2 += 1; window.testInterval1 += 1;
}, },
2000, 100,
undefined, undefined,
'someLevel' 'someLevel'
); );
App.Delay.set(function() {
test('interval - test 2 - 1/2', function() {
// check new Promise( (resolve, reject) => {
equal(window.testInterval2, 4, 'interval - test 2') App.Delay.set(resolve, 1000);
new Promise( (resolve, reject) => {
App.Delay.set(resolve, 2000);
}).then( function() {
equal(window.testInterval1, window.testInterval1Backup, 'interval - did not change after clear interval')
})
.finally(done)
}).then( function() {
notEqual(window.testInterval1, 1, 'interval - interval moved up')
App.Interval.clearLevel('someLevel') App.Interval.clearLevel('someLevel')
}); window.testInterval1Backup = window.testInterval1;
}, })
5200 })
);
App.Delay.set(function() {
test('interval - test 2 - 2/2', function() {
// check // events
equal(window.testInterval2, 4, 'interval - test 2 - after clear') test('events simple', function() {
});
},
6900
);
// events
test('events simple', function() {
// single bind // single bind
App.Event.bind('test1', function(data) { App.Event.bind('test1', function(data) {
@ -290,9 +274,9 @@ window.onload = function() {
ok(false, 'should not be triggered - multi bind') ok(false, 'should not be triggered - multi bind')
}); });
App.Event.trigger('test1-2', { success: true }) App.Event.trigger('test1-2', { success: true })
}); });
test('events level', function() { test('events level', function() {
// bind with level // bind with level
App.Event.bind('test3', function(data) { App.Event.bind('test3', function(data) {
@ -309,10 +293,10 @@ window.onload = function() {
}, 'test-level') }, 'test-level')
App.Event.trigger('test3', { success: true}) App.Event.trigger('test3', { success: true})
}); });
// session store // session store
test('session store', function() { test('session store', function() {
var tests = [ var tests = [
'some 123äöüßadajsdaiosjdiaoidj', 'some 123äöüßadajsdaiosjdiaoidj',
@ -355,10 +339,10 @@ window.onload = function() {
deepEqual(undefined, item, 'write/get/delete - compare deleted data') deepEqual(undefined, item, 'write/get/delete - compare deleted data')
}); });
}); });
// config // config
test('config', function() { test('config', function() {
// simple // simple
var tests = [ var tests = [
@ -397,11 +381,11 @@ window.onload = function() {
var item = App.Config.get(test.key, 'group1') var item = App.Config.get(test.key, 'group1')
deepEqual(item, test.value, 'group set/get tests') deepEqual(item, test.value, 'group set/get tests')
}); });
}); });
// clone // clone
test('clone', function() { test('clone', function() {
// simple // simple
var tests = [ var tests = [
@ -499,10 +483,10 @@ window.onload = function() {
equal(reference[2].fn(), 'test') equal(reference[2].fn(), 'test')
equal(result[2].fn(), 'test') equal(result[2].fn(), 'test')
}); });
// diff // diff
test('difference', function() { test('difference', function() {
// simple // simple
var object1 = { var object1 = {
@ -595,52 +579,46 @@ window.onload = function() {
item = difference(object1, object2) item = difference(object1, object2)
deepEqual(item, result) deepEqual(item, result)
}); });
// auth test('auth - not existing user', function(assert) {
var done = assert.async(1)
new Promise( (resolve, reject) => {
App.Auth.login({ App.Auth.login({
data: { data: {
username: 'not_existing', username: 'not_existing',
password: 'not_existing', password: 'not_existing',
}, },
success: function(data) { success: resolve,
test('auth - not existing user', function() { error: reject
ok(false, 'ok')
})
},
error: function() {
test('auth - not existing user', function() {
ok(true, 'ok')
authWithSession()
})
}
}); });
}).then( function(data) {
ok(false, 'ok')
}, function() {
ok(true, 'ok')
})
.finally(done)
})
var authWithSession = function() { test('auth - existing user', function(assert) {
var done = assert.async(1)
new Promise( (resolve, reject) => {
App.Auth.login({ App.Auth.login({
data: { data: {
username: 'master@example.com', username: 'master@example.com',
password: 'test', password: 'test',
}, },
success: function(data) { success: resolve,
test('auth - existing user', function() { error: reject
});
}).then( function(data) {
ok(true, 'authenticated') ok(true, 'authenticated')
var user = App.Session.get('login') var user = App.Session.get('login')
equal('master@example.com', user, 'session login') equal('master@example.com', user, 'session login')
}, function() {
ok(false, 'failed')
}) })
}, .finally(done)
error: function() { })
test('auth - existing user', function() {
ok(false, 'not authenticated')
})
}
});
}
}
// run tests after the app has been initialized to avoid race condition
// between interval (timing) checks that may delayed by JS parsing
// and App initialization
App.Event.bind("app:ready", callback)
}

View file

@ -10,12 +10,14 @@ class AgentNavigationAndTitleTest < TestCase
) )
tasks_close_all() tasks_close_all()
# since we run the basic functionality tests via Capybara the clues are shown # since we run the basic functionality tests via Capybara now the clues are shown
# and skipped after the login. This removes the 'is-active' class from the # and closed after the login. This unfortunately removes the 'is-active' class from the
# dashboard link causing the following tests to fail. As the browser tests are # dashboard link causing the following tests to fail.
# deprecated and there is no easy fix to change that we refresh the page as # Because the browser tests are deprecated and there is no easy fix to change the
# a workaround. This will cause the 'is-active' class to be set on the menu item again # behavior we refresh the page and wait for it to finish loading the app as a workaround.
# This will cause the 'is-active' class to be set on the menu item again
reload() reload()
sleep 4
# dashboard after login # dashboard after login
verify_title(value: 'dashboard') verify_title(value: 'dashboard')