-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR does three things: - Adds tests for undefined/null properties - Updates test titles - Formats the test suites The new tests were brought on by #146 ## New tests I've added two (three) new tests. 1. `src/util.test.ts`: Verify that object properties set to `null` or `undefined` are excluded from query params 2. `src/index.test.ts`: Verify that setting a context field to `null` or `undefined` removes it from the context ## Test title updates Two of the tests in `src/util.test.ts` seems to have had copy-paste mistakes in their titles. Looking at them, I believe the "not" in their descriptions should be removed. ## Formatting This repo has prettier listed in its deps, but the test files were pretty unformatted. My editor auto-formats on save, so this led to a lot of changes. However, I've isolated the formatting changes into separate commits, so we can easily cut those out if we don't want them. Diffing with **hidden whitespace** is encouraged. ## Commits * fix: reword test names + change ts-ignore to ts-expect-error Looking at the tests, I can only assume that the "not" in their names was a copy-paste error. * format: format test suite * fix: add new test that checks null and undefined properties * fix: add test that clearing context works as expected * format: run prettier on tests * Fix: update ts-expect-error comment * add check for wrapped strings of null or undefined when filtering params * revert --------- Co-authored-by: andreas-unleash <andreas@getunleash.ai>
- Loading branch information
1 parent
857363a
commit f86d918
Showing
3 changed files
with
90 additions
and
34 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
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 |
---|---|---|
@@ -1,28 +1,60 @@ | ||
import {urlWithContextAsQuery} from './util'; | ||
import { urlWithContextAsQuery } from './util'; | ||
|
||
test('should not add paramters to URL', async () => { | ||
const someUrl = new URL("https://test.com"); | ||
const someUrl = new URL('https://test.com'); | ||
|
||
//@ts-ignore on purpose for testing! | ||
//@ts-expect-error on purpose for testing! | ||
const result = urlWithContextAsQuery(someUrl, {}); | ||
|
||
expect(result.toString()).toBe('https://test.com/'); | ||
}); | ||
|
||
test('should add context as query params', async () => { | ||
const someUrl = new URL('https://test.com'); | ||
|
||
test('should not add context as query params', async () => { | ||
const someUrl = new URL("https://test.com"); | ||
const result = urlWithContextAsQuery(someUrl, { | ||
appName: 'test', | ||
userId: '1234A', | ||
}); | ||
|
||
const result = urlWithContextAsQuery(someUrl, {appName: 'test', userId: '1234A'}); | ||
|
||
expect(result.toString()).toBe('https://test.com/?appName=test&userId=1234A'); | ||
expect(result.toString()).toBe( | ||
'https://test.com/?appName=test&userId=1234A' | ||
); | ||
}); | ||
|
||
test('should add context properties as query params', async () => { | ||
const someUrl = new URL('https://test.com'); | ||
|
||
const result = urlWithContextAsQuery(someUrl, { | ||
appName: 'test', | ||
userId: '1234A', | ||
properties: { custom1: 'test', custom2: 'test2' }, | ||
}); | ||
|
||
test('should not add context properties as query params', async () => { | ||
const someUrl = new URL("https://test.com"); | ||
expect(result.toString()).toBe( | ||
'https://test.com/?appName=test&userId=1234A&properties%5Bcustom1%5D=test&properties%5Bcustom2%5D=test2' | ||
); | ||
}); | ||
|
||
const result = urlWithContextAsQuery(someUrl, {appName: 'test', userId: '1234A', properties: { custom1: 'test', custom2: "test2"}}); | ||
test('should exclude context properties that are null or undefined', async () => { | ||
const someUrl = new URL('https://test.com'); | ||
|
||
const result = urlWithContextAsQuery(someUrl, { | ||
appName: 'test', | ||
userId: undefined, | ||
properties: { | ||
custom1: 'test', | ||
custom2: 'test2', | ||
//@ts-expect-error this shouldn't be allowed if you're using TS, but | ||
//you could probably force it | ||
custom3: null, | ||
//@ts-expect-error same as the null property above | ||
custom4: undefined, | ||
}, | ||
}); | ||
|
||
expect(result.toString()).toBe( | ||
'https://test.com/?appName=test&properties%5Bcustom1%5D=test&properties%5Bcustom2%5D=test2' | ||
); | ||
}); | ||
|
||
expect(result.toString()).toBe('https://test.com/?appName=test&userId=1234A&properties%5Bcustom1%5D=test&properties%5Bcustom2%5D=test2'); | ||
}); |
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 |
---|---|---|
|
@@ -29,4 +29,4 @@ export const urlWithContextAsQuery = (url: URL, context: IContext) => { | |
} | ||
}); | ||
return urlWithQuery; | ||
} | ||
} |