Skip to content

Commit

Permalink
Update user tests & give xhr util method name
Browse files Browse the repository at this point in the history
  • Loading branch information
RobAndrewHurst committed Aug 19, 2024
1 parent e2a8bf0 commit f009234
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 49 deletions.
16 changes: 8 additions & 8 deletions lib/utils/_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

// import from node modules
import {render, html, svg} from 'uhtml'
import { render, html, svg } from 'uhtml'

import * as stats from 'simple-statistics'

Expand All @@ -31,11 +31,11 @@ const compose = (...fns) => {

import convert from './convert.mjs'

import {copyToClipboard} from './copyToClipboard.mjs'
import { copyToClipboard } from './copyToClipboard.mjs'

import {dataURLtoBlob} from './dataURLtoBlob.mjs'
import { dataURLtoBlob } from './dataURLtoBlob.mjs'

import {default as hexa} from './hexa.mjs'
import { default as hexa } from './hexa.mjs'

import loadPlugins from './loadPlugins.mjs'

Expand All @@ -45,7 +45,7 @@ import merge from './merge.mjs'

import paramString from './paramString.mjs'

import {polygonIntersectFeatures} from './polygonIntersectFeatures.mjs'
import { polygonIntersectFeatures } from './polygonIntersectFeatures.mjs'

import promiseAll from './promiseAll.mjs'

Expand All @@ -59,9 +59,9 @@ import * as userIndexedDB from './userIndexedDB.mjs'

import * as gazetteer from './gazetteer.mjs'

import {default as verticeGeoms} from './verticeGeoms.mjs'
import { default as verticeGeoms } from './verticeGeoms.mjs'

import xhr from './xhr.mjs'
import { xhr } from './xhr.mjs'

import { formatNumericValue, unformatStringValue } from './numericFormatter.mjs';

Expand All @@ -83,7 +83,7 @@ export default {
hexa,
loadPlugins,
merge,
olScript: ()=>mapp.ol.load(),
olScript: () => mapp.ol.load(),
paramString,
polygonIntersectFeatures,
promiseAll,
Expand Down
85 changes: 44 additions & 41 deletions lib/utils/xhr.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,61 +29,64 @@ The method is assumed to be 'POST' if a params.body is provided.

const requestMap = new Map()

export default params => new Promise(resolve => {
export function xhr(params) {

// Return if params are falsy.
if (!params) {
console.error(`xhr params are falsy.`)
return;
}
return new Promise(resolve => {

// Set params as object with url from string.
params = typeof params === 'string' ? { url: params } : params
// Return if params are falsy.
if (!params) {
console.error(`xhr params are falsy.`)
return;
}

// A request url must be provided.
if (!params.url) {
console.error(`no xhr request url has been provided.`)
return;
};
// Set params as object with url from string.
params = typeof params === 'string' ? { url: params } : params

// Check whether a request with the same params has already been resolved.
if (params.cache && requestMap.has(params)) return resolve(requestMap.get(params))
// A request url must be provided.
if (!params.url) {
console.error(`no xhr request url has been provided.`)
return;
};

// Assign 'GET' as default method if no body is provided.
params.method ??= params.body ? 'POST' : 'GET'
// Check whether a request with the same params has already been resolved.
if (params.cache && requestMap.has(params)) return resolve(requestMap.get(params))

const xhr = new XMLHttpRequest()
// Assign 'GET' as default method if no body is provided.
params.method ??= params.body ? 'POST' : 'GET'

xhr.open(params.method, params.url)
const xhr = new XMLHttpRequest()

// Use requestHeader: null to prevent assignment of requestHeader.
if (params.requestHeader !== null) {
xhr.open(params.method, params.url)

// Butter (spread) over requestHeader.
const requestHeader = {
'Content-Type': 'application/json',
...params.requestHeader
}
// Use requestHeader: null to prevent assignment of requestHeader.
if (params.requestHeader !== null) {

Object.entries(requestHeader).forEach(entry => xhr.setRequestHeader(...entry))
}
// Butter (spread) over requestHeader.
const requestHeader = {
'Content-Type': 'application/json',
...params.requestHeader
}

xhr.responseType = params.responseType || 'json'
Object.entries(requestHeader).forEach(entry => xhr.setRequestHeader(...entry))
}

xhr.onload = e => {
xhr.responseType = params.responseType || 'json'

if (e.target.status >= 400) {
resolve(new Error(e.target.status))
return;
}
xhr.onload = e => {

// Cache the response in the requestMap
params.cache && requestMap.set(params, e.target.response)
if (e.target.status >= 400) {
resolve(new Error(e.target.status))
return;
}

resolve(params.resolveTarget ? e.target : e.target.response)
}
// Cache the response in the requestMap
params.cache && requestMap.set(params, e.target.response)

resolve(params.resolveTarget ? e.target : e.target.response)
}

xhr.onerror = (e) => resolve(new Error(e))
xhr.onerror = (e) => resolve(new Error(e))

xhr.send(params.body)
})
xhr.send(params.body)
})
}
3 changes: 3 additions & 0 deletions tests/browser/local.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import { pluginsTest } from '../plugins/_plugins.test.mjs';
import { setView } from '../utils/view.js';
import { delayFunction } from '../utils/delay.js';
import { apiTest } from './_api.test.mjs';
import { userTest } from '../mod/user/_user.test.js';
// import { booleanTest } from '../lib/ui/locations/entries/boolean.test.mjs';

await userTest.updateTest();

const mapview = await base();
// Run the dictionary Tests
await dictionaryTest.baseDictionaryTest(mapview);
Expand Down
5 changes: 5 additions & 0 deletions tests/mod/user/_user.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { updateTest } from './update.test.mjs';

export const userTest = {
updateTest
}
81 changes: 81 additions & 0 deletions tests/mod/user/update.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

/**
* ## user.updateTest()
* @module user/update
*/

/**
* This function is used as an entry point for the changeEndTest
* @function updateTest
*/
export async function updateTest(mapview) {

await codi.describe('User: update', async () => {

/**
* ### update endpoint should be able to process a body
* The user update endpoint should be able to take a body as the request.
* @function it
*/
await codi.it('update endpoint should be able to process a body', async () => {

let params = {
url: '/test/api/user/update',
body: JSON.stringify(
{
email: '[email protected]',
admin: false,
approved: false,
verified: false
}
)
};

await mapp.utils.xhr(params);

params = {
url: '/test/api/user/update',
body: JSON.stringify(
{
email: '[email protected]',
admin: true,
approved: true,
verified: false
}
)
}

await mapp.utils.xhr(params);

const acl = await mapp.utils.xhr('/test/api/user/list');
const test_user = acl.filter(user => user.email === '[email protected]')[0];

await codi.assertEqual(test_user.email, '[email protected]', 'The users email address should be [email protected]')
await codi.assertFalse(test_user.verified, 'The user should be not verified')
await codi.assertTrue(test_user.admin, 'The user should an admin')
await codi.assertTrue(test_user.approved, 'The user should be approved')

});

/**
### update endpoint should be able to be just params
* The user update endpoint should be able to take a body as the request.
* @function it
*/
await codi.it('update endpoint should be able to be just params', async () => {

const url = `/test/api/user/[email protected]&field=admin&value=false`
await mapp.utils.xhr(url);

const acl = await mapp.utils.xhr('/test/api/user/list');
const test_user = acl.filter(user => user.email === '[email protected]')[0];

await codi.assertEqual(test_user.email, '[email protected]', 'The users email address should be [email protected]')
await codi.assertFalse(test_user.verified, 'The user should be not verified')
await codi.assertFalse(test_user.admin, 'The user should an admin')
await codi.assertTrue(test_user.approved, 'The user should be approved')

});

});
}

0 comments on commit f009234

Please sign in to comment.