-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
louiszawadzki
committed
Sep 20, 2023
1 parent
30ef307
commit 0f2a179
Showing
3 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/react-native-apollo-client/src/__tests__/mappers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { mapVariables } from '../mappers'; | ||
|
||
describe('mappers', () => { | ||
describe('mapVariables', () => { | ||
it('executes the provided mapper on the variables as JSON', () => { | ||
const originalVariables = JSON.stringify({ | ||
user: { | ||
email: 'user@email.com', | ||
password: 'pass', | ||
type: 'admin' | ||
} | ||
}); | ||
const mapper = (variables: Record<string, any> | null) => { | ||
if (variables && variables.user?.email) { | ||
delete variables.user.email; | ||
} | ||
if (variables && variables.user?.password) { | ||
delete variables.user.password; | ||
} | ||
return variables; | ||
}; | ||
|
||
expect(mapVariables(originalVariables, mapper)).toBe( | ||
'{"user":{"type":"admin"}}' | ||
); | ||
}); | ||
it('returns the original variables if they are not well formatted as JSON', () => { | ||
const originalVariables = '{user: [Object object]}'; | ||
const mapper = (variables: Record<string, any> | null) => { | ||
if (variables && variables.user?.email) { | ||
delete variables.user.email; | ||
} | ||
if (variables && variables.user?.password) { | ||
delete variables.user.password; | ||
} | ||
return variables; | ||
}; | ||
|
||
expect(mapVariables(originalVariables, mapper)).toBe( | ||
originalVariables | ||
); | ||
}); | ||
it('returns the original variables if the mapper errors', () => { | ||
const originalVariables = JSON.stringify({ | ||
user: { | ||
email: 'user@email.com', | ||
password: 'pass', | ||
type: 'admin' | ||
} | ||
}); | ||
const mapper = (variables: Record<string, any> | null) => { | ||
if (variables && variables.user?.email) { | ||
delete variables.user.email; | ||
} | ||
if (variables && variables.user?.password) { | ||
delete variables.user.password; | ||
} | ||
throw new Error('mapper error'); | ||
}; | ||
|
||
expect(mapVariables(originalVariables, mapper)).toBe( | ||
originalVariables | ||
); | ||
}); | ||
it('returns the original variables if no mapper was provided', () => { | ||
const originalVariables = JSON.stringify({ | ||
user: { | ||
email: 'user@email.com', | ||
password: 'pass', | ||
type: 'admin' | ||
} | ||
}); | ||
|
||
expect(mapVariables(originalVariables)).toBe(originalVariables); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export type VariablesMapper = ( | ||
variables: Record<string, unknown> | null | ||
) => Record<string, unknown> | null; | ||
|
||
export const mapVariables = ( | ||
originalVariables: string | null, | ||
mapper?: VariablesMapper | ||
): string | null => { | ||
if (!mapper) { | ||
return originalVariables; | ||
} | ||
try { | ||
const mappedVariables = mapper( | ||
originalVariables === null ? null : JSON.parse(originalVariables) | ||
); | ||
|
||
return JSON.stringify(mappedVariables); | ||
} catch (e) { | ||
// TODO: telemetry | ||
return originalVariables; | ||
} | ||
}; |