@eduzz/accounts-react-wrapper
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -73,3 +73,6 @@ "use strict"; | ||
var refreshToken = faker_1.datatype.uuid(); | ||
var bearerToken = faker_1.datatype.uuid(); | ||
var bearerToken = jsonwebtoken_1.default.sign({ | ||
name: faker_1.random.word(), | ||
email: faker_1.internet.email(), | ||
}, faker_1.random.word()); | ||
var accountsToken = faker_1.datatype.uuid(); | ||
@@ -79,3 +82,3 @@ var refreshResponseDefinitions = { | ||
refreshTokenKey: faker_1.random.word(), | ||
mapData: 'data.dados', | ||
mapData: '', | ||
}; | ||
@@ -85,3 +88,3 @@ var loginResponseDefinitions = { | ||
refreshTokenKey: faker_1.random.word(), | ||
mapData: 'data.dados', | ||
mapData: '', | ||
}; | ||
@@ -232,3 +235,3 @@ var response = { | ||
}); }); | ||
it('shoud set user, loading and save refresh token on cookie when try refresh is success', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
it('shoud set bearerTokenDecoded, loading and save refresh token on cookie when try refresh is success', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, result, unmount; | ||
@@ -242,8 +245,6 @@ var _b; | ||
}), result = _a.result, unmount = _a.unmount; | ||
jest.spyOn(service_1.default, 'refreshToken').mockResolvedValue(__assign(__assign({}, response), { data: { | ||
dados: (_b = {}, | ||
_b[refreshResponseDefinitions.tokenKey] = bearerToken, | ||
_b[refreshResponseDefinitions.refreshTokenKey] = refreshToken, | ||
_b), | ||
} })); | ||
jest.spyOn(service_1.default, 'refreshToken').mockResolvedValue(__assign(__assign({}, response), { data: (_b = {}, | ||
_b[refreshResponseDefinitions.tokenKey] = bearerToken, | ||
_b[refreshResponseDefinitions.refreshTokenKey] = refreshToken, | ||
_b) })); | ||
jest.spyOn(jsonwebtoken_1.default, 'decode').mockReturnValue(bearerToken); | ||
@@ -313,3 +314,3 @@ return [4 /*yield*/, (0, test_tools_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
it('should call tryLogin, then save token and set user', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, result, unmount; | ||
var _a, result, unmount, bearerTokenDecoded; | ||
var _b; | ||
@@ -322,9 +323,8 @@ return __generator(this, function (_c) { | ||
}), result = _a.result, unmount = _a.unmount; | ||
jest.spyOn(service_1.default, 'login').mockResolvedValue(__assign(__assign({}, response), { data: { | ||
dados: (_b = {}, | ||
_b[loginResponseDefinitions.tokenKey] = bearerToken, | ||
_b[loginResponseDefinitions.refreshTokenKey] = refreshToken, | ||
_b), | ||
} })); | ||
jest.spyOn(jsonwebtoken_1.default, 'decode').mockReturnValue(bearerToken); | ||
jest.spyOn(service_1.default, 'login').mockResolvedValue(__assign(__assign({}, response), { data: (_b = {}, | ||
_b[loginResponseDefinitions.tokenKey] = bearerToken, | ||
_b[loginResponseDefinitions.refreshTokenKey] = refreshToken, | ||
_b) })); | ||
bearerTokenDecoded = jsonwebtoken_1.default.decode(bearerToken); | ||
jest.spyOn(jsonwebtoken_1.default, 'decode').mockReturnValue(bearerTokenDecoded); | ||
return [4 /*yield*/, (0, test_tools_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -347,3 +347,3 @@ return __generator(this, function (_a) { | ||
expect(mocks_1.saveRefreshTokenOnCookieFn).toBeCalledWith(refreshToken); | ||
expect(result.current.bearerTokenDecoded).toBe(bearerToken); | ||
expect(result.current.bearerTokenDecoded).toBe(bearerTokenDecoded); | ||
expect(result.current.bearerToken).toBe(bearerToken); | ||
@@ -350,0 +350,0 @@ expect(result.current.loading).toBe(false); |
@@ -34,3 +34,3 @@ /// <reference types="react" /> | ||
refreshTokenKey: string; | ||
mapData: string; | ||
mapData?: string; | ||
}; | ||
@@ -49,3 +49,3 @@ export declare type LoginServiceType = { | ||
refreshTokenKey: string; | ||
mapData: string; | ||
mapData?: string; | ||
}; | ||
@@ -52,0 +52,0 @@ export declare type RefreshServiceType = { |
declare function getRefreshTokenFromCookie(): string | undefined; | ||
declare function saveRefreshTokenOnCookie(refreshToken: string): void; | ||
declare function eraseRefreshTokenOnCookie(): void; | ||
declare function findTarget(mapData: string, apiResultData: any): any; | ||
declare function findTarget(mapData: undefined | string, apiResultData: any): any; | ||
export { getRefreshTokenFromCookie, saveRefreshTokenOnCookie, eraseRefreshTokenOnCookie, findTarget, }; |
@@ -21,6 +21,6 @@ "use strict"; | ||
function findTarget(mapData, apiResultData) { | ||
return mapData.split('.').reduce(function (acc, curr) { | ||
return ((mapData === null || mapData === void 0 ? void 0 : mapData.split('.').reduce(function (acc, curr) { | ||
return (acc === null || acc === void 0 ? void 0 : acc[curr]) || apiResultData[curr]; | ||
}, {}); | ||
}, {})) || apiResultData); | ||
} | ||
exports.findTarget = findTarget; |
@@ -22,5 +22,5 @@ "use strict"; | ||
}); | ||
it('should pass mapData with array and return map object', function () { | ||
it('should pass mapData as data.dados.0.map.0 and return mapped object', function () { | ||
var mapData = 'data.dados.0.map.0'; | ||
var apiReulst = { | ||
var apiResult = { | ||
data: { | ||
@@ -39,12 +39,44 @@ dados: [ | ||
}; | ||
var expectResut = { | ||
var expectedResult = { | ||
name: '123', | ||
jogo: '566', | ||
}; | ||
var result = (0, utils_1.findTarget)(mapData, apiReulst); | ||
expect(result).toStrictEqual(expectResut); | ||
var result = (0, utils_1.findTarget)(mapData, apiResult); | ||
expect(result).toStrictEqual(expectedResult); | ||
}); | ||
it('should pass mapData as data and return mapped object', function () { | ||
var mapData = 'data'; | ||
var apiResult = { | ||
data: { | ||
name: '123', | ||
jogo: '566', | ||
}, | ||
}; | ||
var expectResult = { | ||
name: '123', | ||
jogo: '566', | ||
}; | ||
var result = (0, utils_1.findTarget)(mapData, apiResult); | ||
expect(result).toEqual(expectResult); | ||
}); | ||
it('should pass mapData as empty and return mapped object', function () { | ||
var mapData = ''; | ||
var apiResult = { | ||
data: { | ||
name: '123', | ||
jogo: '566', | ||
}, | ||
}; | ||
var expectedResult = { | ||
data: { | ||
name: '123', | ||
jogo: '566', | ||
}, | ||
}; | ||
var result = (0, utils_1.findTarget)(mapData, apiResult); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
it('should pass mapData with only one object inside and return map object', function () { | ||
var mapData = 'data.dados'; | ||
var apiReulst = { | ||
var apiResult = { | ||
data: { | ||
@@ -57,9 +89,9 @@ dados: { | ||
}; | ||
var expectResut = { | ||
var expectedResult = { | ||
name: '123', | ||
jogo: '566', | ||
}; | ||
var result = (0, utils_1.findTarget)(mapData, apiReulst); | ||
expect(result).toStrictEqual(expectResut); | ||
var result = (0, utils_1.findTarget)(mapData, apiResult); | ||
expect(result).toStrictEqual(expectedResult); | ||
}); | ||
}); |
@@ -68,3 +68,6 @@ var __assign = (this && this.__assign) || function () { | ||
var refreshToken = datatype.uuid(); | ||
var bearerToken = datatype.uuid(); | ||
var bearerToken = Jwt.sign({ | ||
name: random.word(), | ||
email: internet.email(), | ||
}, random.word()); | ||
var accountsToken = datatype.uuid(); | ||
@@ -74,3 +77,3 @@ var refreshResponseDefinitions = { | ||
refreshTokenKey: random.word(), | ||
mapData: 'data.dados', | ||
mapData: '', | ||
}; | ||
@@ -80,3 +83,3 @@ var loginResponseDefinitions = { | ||
refreshTokenKey: random.word(), | ||
mapData: 'data.dados', | ||
mapData: '', | ||
}; | ||
@@ -227,3 +230,3 @@ var response = { | ||
}); }); | ||
it('shoud set user, loading and save refresh token on cookie when try refresh is success', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
it('shoud set bearerTokenDecoded, loading and save refresh token on cookie when try refresh is success', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, result, unmount; | ||
@@ -237,8 +240,6 @@ var _b; | ||
}), result = _a.result, unmount = _a.unmount; | ||
jest.spyOn(Api, 'refreshToken').mockResolvedValue(__assign(__assign({}, response), { data: { | ||
dados: (_b = {}, | ||
_b[refreshResponseDefinitions.tokenKey] = bearerToken, | ||
_b[refreshResponseDefinitions.refreshTokenKey] = refreshToken, | ||
_b), | ||
} })); | ||
jest.spyOn(Api, 'refreshToken').mockResolvedValue(__assign(__assign({}, response), { data: (_b = {}, | ||
_b[refreshResponseDefinitions.tokenKey] = bearerToken, | ||
_b[refreshResponseDefinitions.refreshTokenKey] = refreshToken, | ||
_b) })); | ||
jest.spyOn(Jwt, 'decode').mockReturnValue(bearerToken); | ||
@@ -308,3 +309,3 @@ return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
it('should call tryLogin, then save token and set user', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, result, unmount; | ||
var _a, result, unmount, bearerTokenDecoded; | ||
var _b; | ||
@@ -317,9 +318,8 @@ return __generator(this, function (_c) { | ||
}), result = _a.result, unmount = _a.unmount; | ||
jest.spyOn(Api, 'login').mockResolvedValue(__assign(__assign({}, response), { data: { | ||
dados: (_b = {}, | ||
_b[loginResponseDefinitions.tokenKey] = bearerToken, | ||
_b[loginResponseDefinitions.refreshTokenKey] = refreshToken, | ||
_b), | ||
} })); | ||
jest.spyOn(Jwt, 'decode').mockReturnValue(bearerToken); | ||
jest.spyOn(Api, 'login').mockResolvedValue(__assign(__assign({}, response), { data: (_b = {}, | ||
_b[loginResponseDefinitions.tokenKey] = bearerToken, | ||
_b[loginResponseDefinitions.refreshTokenKey] = refreshToken, | ||
_b) })); | ||
bearerTokenDecoded = Jwt.decode(bearerToken); | ||
jest.spyOn(Jwt, 'decode').mockReturnValue(bearerTokenDecoded); | ||
return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -342,3 +342,3 @@ return __generator(this, function (_a) { | ||
expect(saveRefreshTokenOnCookieFn).toBeCalledWith(refreshToken); | ||
expect(result.current.bearerTokenDecoded).toBe(bearerToken); | ||
expect(result.current.bearerTokenDecoded).toBe(bearerTokenDecoded); | ||
expect(result.current.bearerToken).toBe(bearerToken); | ||
@@ -345,0 +345,0 @@ expect(result.current.loading).toBe(false); |
@@ -34,3 +34,3 @@ /// <reference types="react" /> | ||
refreshTokenKey: string; | ||
mapData: string; | ||
mapData?: string; | ||
}; | ||
@@ -49,3 +49,3 @@ export declare type LoginServiceType = { | ||
refreshTokenKey: string; | ||
mapData: string; | ||
mapData?: string; | ||
}; | ||
@@ -52,0 +52,0 @@ export declare type RefreshServiceType = { |
declare function getRefreshTokenFromCookie(): string | undefined; | ||
declare function saveRefreshTokenOnCookie(refreshToken: string): void; | ||
declare function eraseRefreshTokenOnCookie(): void; | ||
declare function findTarget(mapData: string, apiResultData: any): any; | ||
declare function findTarget(mapData: undefined | string, apiResultData: any): any; | ||
export { getRefreshTokenFromCookie, saveRefreshTokenOnCookie, eraseRefreshTokenOnCookie, findTarget, }; |
@@ -15,6 +15,6 @@ function getRefreshTokenFromCookie() { | ||
function findTarget(mapData, apiResultData) { | ||
return mapData.split('.').reduce(function (acc, curr) { | ||
return ((mapData === null || mapData === void 0 ? void 0 : mapData.split('.').reduce(function (acc, curr) { | ||
return (acc === null || acc === void 0 ? void 0 : acc[curr]) || apiResultData[curr]; | ||
}, {}); | ||
}, {})) || apiResultData); | ||
} | ||
export { getRefreshTokenFromCookie, saveRefreshTokenOnCookie, eraseRefreshTokenOnCookie, findTarget, }; |
@@ -20,5 +20,5 @@ import { random } from 'faker'; | ||
}); | ||
it('should pass mapData with array and return map object', function () { | ||
it('should pass mapData as data.dados.0.map.0 and return mapped object', function () { | ||
var mapData = 'data.dados.0.map.0'; | ||
var apiReulst = { | ||
var apiResult = { | ||
data: { | ||
@@ -37,12 +37,44 @@ dados: [ | ||
}; | ||
var expectResut = { | ||
var expectedResult = { | ||
name: '123', | ||
jogo: '566', | ||
}; | ||
var result = findTarget(mapData, apiReulst); | ||
expect(result).toStrictEqual(expectResut); | ||
var result = findTarget(mapData, apiResult); | ||
expect(result).toStrictEqual(expectedResult); | ||
}); | ||
it('should pass mapData as data and return mapped object', function () { | ||
var mapData = 'data'; | ||
var apiResult = { | ||
data: { | ||
name: '123', | ||
jogo: '566', | ||
}, | ||
}; | ||
var expectResult = { | ||
name: '123', | ||
jogo: '566', | ||
}; | ||
var result = findTarget(mapData, apiResult); | ||
expect(result).toEqual(expectResult); | ||
}); | ||
it('should pass mapData as empty and return mapped object', function () { | ||
var mapData = ''; | ||
var apiResult = { | ||
data: { | ||
name: '123', | ||
jogo: '566', | ||
}, | ||
}; | ||
var expectedResult = { | ||
data: { | ||
name: '123', | ||
jogo: '566', | ||
}, | ||
}; | ||
var result = findTarget(mapData, apiResult); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
it('should pass mapData with only one object inside and return map object', function () { | ||
var mapData = 'data.dados'; | ||
var apiReulst = { | ||
var apiResult = { | ||
data: { | ||
@@ -55,9 +87,9 @@ dados: { | ||
}; | ||
var expectResut = { | ||
var expectedResult = { | ||
name: '123', | ||
jogo: '566', | ||
}; | ||
var result = findTarget(mapData, apiReulst); | ||
expect(result).toStrictEqual(expectResut); | ||
var result = findTarget(mapData, apiResult); | ||
expect(result).toStrictEqual(expectedResult); | ||
}); | ||
}); |
{ | ||
"name": "@eduzz/accounts-react-wrapper", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"main": "./lib/cjs/index.js", | ||
@@ -5,0 +5,0 @@ "module": "./lib/esm/index.js", |
@@ -125,3 +125,3 @@ # Eduzz Accounts React Wrapper | ||
- mapData: string do path que contém as respostas da api. ex: data.dados | ||
- mapData(opcional): string do path que contém as respostas da api. ex: data.dados | ||
@@ -140,3 +140,3 @@ 2. Refresh | ||
- mapData: string do path que contém as respostas da api. ex: data.dados | ||
- mapData(opcional): string do path que contém as respostas da api. ex: data.dados | ||
@@ -175,3 +175,3 @@ */ | ||
function App() { | ||
const { bearerTokenDedoded, loading, bearerToken, logout } = useAuth<ExapledBearerTokenDecoded>(); | ||
const { bearerTokenDecoded, loading, bearerToken, logout } = useAuth<ExapledBearerTokenDecoded>(); | ||
@@ -182,4 +182,4 @@ return ( | ||
<div className="App"> | ||
<p>user name: {bearerTokenDedoded?.name}</p> | ||
<p>user email: {bearerTokenDedoded?.email}</p> | ||
<p>user name: {bearerTokenDecoded?.name}</p> | ||
<p>user email: {bearerTokenDecoded?.email}</p> | ||
<p>bearerToken: {bearerToken}</p> | ||
@@ -186,0 +186,0 @@ <p>loading: {loading}</p> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
121105
2493