Extract and test a couple of helper functions

This commit is contained in:
Kyle Corbelli 2017-09-15 11:21:02 -07:00
parent 56c3af6013
commit a18dada81c
7 changed files with 86 additions and 60 deletions

View File

@ -37,10 +37,11 @@ import {
SignOutRequestFailedAction,
} from './types'
import {
setAuthHeaders,
deleteAuthHeaders,
persistAuthHeadersInLocalStorage,
deleteAuthHeadersFromLocalStorage,
getUserAttributesFromResponse,
persistAuthHeadersInLocalStorage,
setAuthHeaders,
} from './services/auth' // <- maybe this is where you pass in the platform paramter, specifying if it is for a browser or for React Native
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -121,29 +122,6 @@ export const signOutRequestFailed = (): SignOutRequestFailedAction => ({
// },
// }
// extract this service somewhere and unit test it:
const invertHash = (hash: { [key: string]: any }) => {
const newHash = {}
for (let key in hash) {
const val = hash[key]
newHash[val] = key
}
return newHash
}
// extract this service somewhere and unit test it:
const getUserAttributesFromResponse = (userAttributes: any, response: any) => {
const invertedUserAttributes = invertHash(userAttributes)
const userAttributesBackendKeys = Object.keys(invertedUserAttributes)
const userAttributesToReturn = {}
Object.keys(response.data.data).forEach((key: string) => {
if (userAttributesBackendKeys.indexOf(key) !== -1) {
userAttributesToReturn[invertedUserAttributes[key]] = response.data.data[key]
}
})
return userAttributesToReturn
}
const generateAuthActions = (config: { [key: string]: any }): ActionsExport => {
const {
authUrl,

View File

@ -1,32 +0,0 @@
"use strict";
exports.__esModule = true;
var axios_1 = require("axios");
var authHeaderKeys = [
'access-token',
'token-type',
'client',
'expiry',
'uid',
];
exports.setAuthHeaders = function (headers) {
authHeaderKeys.forEach(function (key) {
axios_1["default"].defaults.headers.common[key] = headers[key];
});
};
// Will have to take a parameter from the package user to determine if this is for a browser or for React Native:
exports.persistAuthHeadersInLocalStorage = function (headers) {
authHeaderKeys.forEach(function (key) {
localStorage.setItem(key, headers[key]);
});
};
exports.deleteAuthHeaders = function () {
authHeaderKeys.forEach(function (key) {
delete axios_1["default"].defaults.headers.common[key];
});
};
// Will have to take a parameter from the package user to determine if this is for a browser or for React Native:
exports.deleteAuthHeadersFromLocalStorage = function () {
authHeaderKeys.forEach(function (key) {
localStorage.removeItem(key);
});
};

View File

@ -1,8 +1,13 @@
import axios from 'axios'
import { AuthHeaders } from '../types'
import {
setAuthHeaders,
AuthHeaders,
AuthResponse,
SingleLayerStringMap,
} from '../types'
import {
deleteAuthHeaders,
getUserAttributesFromResponse,
setAuthHeaders,
} from './auth'
describe('auth service', () => {
@ -31,4 +36,28 @@ describe('auth service', () => {
})
})
})
describe('getUserAttributesFromResponse', () => {
it('gets the values of the user attributes from the response, accounting for casing differences', () => {
const userAttributes: SingleLayerStringMap = {
firstName: 'first_name',
lastName: 'last_name',
}
const authResponse: AuthResponse = {
headers,
data: {
data: {
first_name: 'Rick',
last_name: 'Sanchez',
},
},
}
const result: SingleLayerStringMap = getUserAttributesFromResponse(userAttributes, authResponse)
const expectedResult: SingleLayerStringMap = {
firstName: 'Rick',
lastName: 'Sanchez',
}
expect(result).toEqual(expectedResult)
})
})
})

View File

@ -1,5 +1,10 @@
import axios from 'axios'
import { AuthHeaders } from '../types'
import { invertMapKeysAndValues } from './utility'
import {
AuthHeaders,
AuthResponse,
SingleLayerStringMap,
} from '../types'
const authHeaderKeys: Array<string> = [
'access-token',
@ -34,3 +39,18 @@ export const deleteAuthHeadersFromLocalStorage = (): void => {
localStorage.removeItem(key)
})
}
export const getUserAttributesFromResponse = (
userAttributes: SingleLayerStringMap,
response: AuthResponse
): SingleLayerStringMap => {
const invertedUserAttributes: SingleLayerStringMap = invertMapKeysAndValues(userAttributes)
const userAttributesBackendKeys: string[] = Object.keys(invertedUserAttributes)
const userAttributesToReturn: SingleLayerStringMap = {}
Object.keys(response.data.data).forEach((key: string) => {
if (userAttributesBackendKeys.indexOf(key) !== -1) {
userAttributesToReturn[invertedUserAttributes[key]] = response.data.data[key]
}
})
return userAttributesToReturn
}

View File

@ -0,0 +1,17 @@
import { SingleLayerStringMap } from '../types'
import { invertMapKeysAndValues } from './utility'
describe('invertMapKeysAndValues', () => {
it('sets the values as the keys and the keys as the values', () => {
const stringMap: SingleLayerStringMap = {
firstName: 'first_name',
last_name: 'lastName',
}
const result: SingleLayerStringMap = invertMapKeysAndValues(stringMap)
const expectedResult: SingleLayerStringMap = {
first_name: 'firstName',
lastName: 'last_name',
}
expect(result).toEqual(expectedResult)
})
})

10
src/services/utility.ts Normal file
View File

@ -0,0 +1,10 @@
import { SingleLayerStringMap } from '../types'
export const invertMapKeysAndValues = (stringMap: SingleLayerStringMap): SingleLayerStringMap => {
const newStringMap: SingleLayerStringMap = {}
for (let key in stringMap) {
const val = stringMap[key]
newStringMap[val] = key
}
return newStringMap
}

View File

@ -173,3 +173,7 @@ export interface ActionsExport {
}
export type ActionsGeneratorExport = (config: { [key: string]: any }) => ActionsExport
export interface SingleLayerStringMap {
[key: string]: string
}