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,259 +1,243 @@
window.onload = function() {
test( "ajax get 200", function(assert) {
var done = assert.async(1)
callback = function() {
// ajax
new Promise( (resolve, reject) => {
App.Ajax.request({
type: 'GET',
url: '/assets/tests/ajax-test.json',
success: function (data) {
test( "ajax get 200", function() {
success: resolve,
error: reject
});
}).then( function(data) {
ok( true, "File found!")
equal(data.success, true, "content parsable and ok!")
equal(data.success2, undefined, "content parsable and ok!")
});
},
error: function (data) {
test( "ajax", function() {
}, function(data) {
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({
type: 'GET',
url: '/tests/wait/2',
queue: true,
success: function (data) {
test( "ajax - queue - ajax get 200 1/2", function() {
// check queue
success: resolve,
error: reject
});
}).then( function(data) {
ok( !window.testAjax, 'ajax - queue - check queue')
window.testAjax = true;
equal(data.success, true, "ajax - queue - content parsable and ok!")
equal(data.success2, undefined, "ajax - queue - content parsable and ok!")
});
},
error: function (data) {
test( "ajax", function() {
}, function(data) {
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({
type: 'GET',
url: '/tests/wait/1',
queue: true,
success: function (data) {
test( "ajax - queue - ajax get 200 2/2", function() {
success: resolve,
error: reject
});
}).then( function(data) {
// check queue
ok( window.testAjax, 'ajax - queue - check queue')
window.testAjax = undefined;
equal(data.success, true, "content parsable and ok!")
equal(data.success2, undefined, "content parsable and ok!")
});
},
error: function (data) {
test( "ajax", function() {
}, function(data) {
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({
type: 'GET',
url: '/tests/wait/3',
success: function (data) {
test( "ajax - parallel - ajax get 200 1/2", function() {
success: resolve,
error: reject
});
// 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!")
});
},
error: function (data) {
test( "ajax", function() {
ok( false, "Failed!")
});
}
});
new Promise( (resolve, reject) => {
App.Ajax.request({
type: 'GET',
url: '/tests/wait/1',
success: function (data) {
test( "ajax - parallel - ajax get 200 2/2", function() {
success: resolve,
error: reject
});
}).then( function(data) {
// check queue
ok( !window.testAjaxQ, 'ajax - parallel - check queue')
window.testAjaxQ = true;
equal(data.success, true, "content parsable and ok!")
equal(data.success2, undefined, "content parsable and ok!")
});
},
error: function (data) {
test( "ajax", function() {
}, function(data) {
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!")
})
});
// delay
test('delay - test', function(assert) {
var done = assert.async(1)
window.testDelay1 = false
App.Delay.set(function() {
test('delay - test 1 - 1/3 - should not be executed, will be reset by next set()', function() {
new Promise( (resolve, reject) => {
App.Delay.set(resolve, 1000, 'delay-test1', 'level');
// check
ok(false, 'delay - test 1 - 1/3 - should not be executed, will be reset by next set()')
window.testDelay1 = true;
});
},
1000,
'delay-test1',
'level'
);
App.Delay.set(function() {
test('delay - test 1 - 2/3', function() {
// check
ok(!window.testDelay1, 'delay - test 1 - 2/3')
new Promise( (resolve, reject) => {
App.Delay.set(resolve, 2000, 'delay-test1', 'level');
}).then( function() {
ok(!window.testDelay1, 'delay - 1/2')
window.testDelay1 = 1;
});
},
2000,
'delay-test1',
'level'
);
App.Delay.set(function() {
test('delay - test 1 - 2/3', function() {
})
// check
ok(window.testDelay1, 'delay - test 1 - 2/3')
new Promise( (resolve, reject) => {
App.Delay.set(resolve, 3000, 'delay-test1-verify', 'level');
}).then( function() {
ok(window.testDelay1, 'delay - 2/2')
window.testDelay1 = false;
})
.finally(done)
}).then( function() {
ok(false, 'delay - 1/2 - FAILED - should not be executed, will be reset by next set()')
window.testDelay1 = true;
})
});
},
3000,
'delay-test1-verify',
'level'
);
App.Delay.set(function() {
test('delay - test 2 - 1/3', function() {
test('delay - test 2', function(assert) {
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')
window.testDelay2 = 1;
});
},
2000
);
App.Delay.set(function() {
test('delay - test 2 - 2/3', function() {
})
// check
ok(!window.testDelay2, 'delay - test 2 - 2/3')
});
},
1000
);
App.Delay.set(function() {
test('delay - test 2 - 3/3', function() {
// check
new Promise( (resolve, reject) => {
App.Delay.set(resolve, 3000);
}).then( function() {
ok(window.testDelay2, 'delay - test 2 - 3/3')
})
.finally(done)
}).then( function() {
ok(!window.testDelay2, 'delay - test 2 - 2/3')
window.testDelay2 = 1;
})
});
},
3000
);
window.testDelay3 = 1;
App.Delay.set(function() {
test('delay - test 3 - 1/1', function() {
test('delay - test 3', function(assert) {
var done = assert.async(1)
// check
ok(false, 'delay - test 3 - 1/1')
});
},
1000,
'delay3'
);
new Promise( (resolve, reject) => {
App.Delay.set(resolve, 1000, 'delay3');
App.Delay.clear('delay3')
App.Delay.set(function() {
test('delay - test 4 - 1/1', function() {
// check
ok(false, 'delay - test 4 - 1/1')
ok(true, 'delay - test 3 - 1/1')
done()
}).then( function() {
ok(false, 'delay - test 3 - 1/1 - FAILED')
})
});
},
1000,
undefined,
'Page'
);
test('delay - test 4', function(assert) {
var done = assert.async(1)
new Promise( (resolve, reject) => {
App.Delay.set(resolve, 1000, undefined, '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
App.Interval.set(function() {
window.testInterval1 += 1;
},
2000,
100,
'interval-test1'
);
App.Delay.set(function() {
test('interval - test 1 - 1/2', function() {
// check
equal(window.testInterval1, 4, 'interval - test 1')
new Promise( (resolve, reject) => {
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')
});
},
5200
);
App.Delay.set(function() {
test('interval - test 1 - 2/2', function() {
window.testInterval1Backup = window.testInterval1;
})
})
// check
equal(window.testInterval1, 4, 'interval - test after clear')
});
},
6500
);
test('interval - test 2', function(assert) {
var done = assert.async(1)
// interval 2
window.testInterval2 = 1
window.testInterval1 = 1
App.Interval.set(function() {
window.testInterval2 += 1;
window.testInterval1 += 1;
},
2000,
100,
undefined,
'someLevel'
);
App.Delay.set(function() {
test('interval - test 2 - 1/2', function() {
// check
equal(window.testInterval2, 4, 'interval - test 2')
new Promise( (resolve, reject) => {
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')
});
},
5200
);
App.Delay.set(function() {
test('interval - test 2 - 2/2', function() {
// check
equal(window.testInterval2, 4, 'interval - test 2 - after clear')
});
},
6900
);
window.testInterval1Backup = window.testInterval1;
})
})
// events
test('events simple', function() {
@ -597,50 +581,44 @@ window.onload = function() {
});
// auth
test('auth - not existing user', function(assert) {
var done = assert.async(1)
new Promise( (resolve, reject) => {
App.Auth.login({
data: {
username: 'not_existing',
password: 'not_existing',
},
success: function(data) {
test('auth - not existing user', function() {
ok(false, 'ok')
})
},
error: function() {
test('auth - not existing user', function() {
ok(true, 'ok')
authWithSession()
})
}
success: resolve,
error: reject
});
}).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({
data: {
username: 'master@example.com',
password: 'test',
},
success: function(data) {
test('auth - existing user', function() {
success: resolve,
error: reject
});
}).then( function(data) {
ok(true, 'authenticated')
var user = App.Session.get('login')
equal('master@example.com', user, 'session login')
}, function() {
ok(false, 'failed')
})
},
error: function() {
test('auth - existing user', function() {
ok(false, 'not authenticated')
.finally(done)
})
}
});
}
}
// 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()
# since we run the basic functionality tests via Capybara the clues are shown
# and skipped after the login. This removes the 'is-active' class from the
# dashboard link causing the following tests to fail. As the browser tests are
# deprecated and there is no easy fix to change that we refresh the page as
# a workaround. This will cause the 'is-active' class to be set on the menu item again
# since we run the basic functionality tests via Capybara now the clues are shown
# and closed after the login. This unfortunately removes the 'is-active' class from the
# dashboard link causing the following tests to fail.
# Because the browser tests are deprecated and there is no easy fix to change the
# 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()
sleep 4
# dashboard after login
verify_title(value: 'dashboard')