-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Picker - formatter settings (#1907)
- useFormatter hook + optional settings prop in jsapi Picker - Fixed a bug with `bindAllMethods` function. It now excludes getters resolves #1889
- Loading branch information
Showing
10 changed files
with
272 additions
and
14 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
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,69 @@ | ||
import { useApi } from '@deephaven/jsapi-bootstrap'; | ||
import { bindAllMethods, TestUtils } from '@deephaven/utils'; | ||
import { | ||
createFormatterFromSettings, | ||
Formatter, | ||
Settings, | ||
} from '@deephaven/jsapi-utils'; | ||
import type { dh as DhType } from '@deephaven/jsapi-types'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useFormatter } from './useFormatter'; | ||
|
||
jest.mock('@deephaven/jsapi-bootstrap'); | ||
jest.mock('@deephaven/jsapi-utils', () => { | ||
const actual = jest.requireActual('@deephaven/jsapi-utils'); | ||
return { | ||
...actual, | ||
createFormatterFromSettings: jest.fn(), | ||
}; | ||
}); | ||
jest.mock('@deephaven/utils', () => ({ | ||
...jest.requireActual('@deephaven/utils'), | ||
bindAllMethods: jest.fn(), | ||
})); | ||
|
||
const { asMock, createMockProxy } = TestUtils; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
expect.hasAssertions(); | ||
}); | ||
|
||
describe('useFormatter', () => { | ||
const mock = { | ||
dh: createMockProxy<typeof DhType>(), | ||
formatter: createMockProxy<Formatter>(), | ||
settings: createMockProxy<Settings>(), | ||
}; | ||
|
||
beforeEach(() => { | ||
asMock(useApi).mockReturnValue(mock.dh); | ||
|
||
asMock(bindAllMethods) | ||
.mockName('bindAllMethods') | ||
.mockImplementation(a => a); | ||
|
||
asMock(createFormatterFromSettings) | ||
.mockName('createFormatterFromSettings') | ||
.mockReturnValue(mock.formatter); | ||
}); | ||
|
||
it('should return members of a `Formatter` instance based on settings', () => { | ||
const { result } = renderHook(() => useFormatter(mock.settings)); | ||
|
||
expect(createFormatterFromSettings).toHaveBeenCalledWith( | ||
mock.dh, | ||
mock.settings | ||
); | ||
|
||
expect(bindAllMethods).toHaveBeenCalledWith(mock.formatter); | ||
|
||
expect(result.current).toEqual({ | ||
getColumnFormat: mock.formatter.getColumnFormat, | ||
getColumnFormatMapForType: mock.formatter.getColumnFormatMapForType, | ||
getColumnTypeFormatter: mock.formatter.getColumnTypeFormatter, | ||
getFormattedString: mock.formatter.getFormattedString, | ||
timeZone: mock.formatter.timeZone, | ||
}); | ||
}); | ||
}); |
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,55 @@ | ||
import { useApi } from '@deephaven/jsapi-bootstrap'; | ||
import { | ||
createFormatterFromSettings, | ||
Formatter, | ||
Settings, | ||
} from '@deephaven/jsapi-utils'; | ||
import { bindAllMethods } from '@deephaven/utils'; | ||
import { useMemo } from 'react'; | ||
|
||
export type UseFormatterResult = Pick< | ||
Formatter, | ||
| 'getColumnFormat' | ||
| 'getColumnFormatMapForType' | ||
| 'getColumnTypeFormatter' | ||
| 'getFormattedString' | ||
| 'timeZone' | ||
>; | ||
|
||
/** | ||
* Returns a subset of members of a `Formatter` instance. The `Formatter` will be | ||
* constructed based on the given options or fallback to the configuration found | ||
* in the current `FormatSettingsContext`. Members that are functions are bound | ||
* to the `Formatter` instance, so they are safe to destructure. Static methods | ||
* can still be accessed statically from the `Formatter` class. | ||
* @param settings Optional settings to use when constructing the `Formatter` | ||
*/ | ||
export function useFormatter(settings?: Settings): UseFormatterResult { | ||
const dh = useApi(); | ||
|
||
const formatter = useMemo(() => { | ||
const instance = createFormatterFromSettings(dh, settings); | ||
|
||
// Bind all methods so we can destructure them | ||
bindAllMethods(instance); | ||
|
||
return instance; | ||
}, [dh, settings]); | ||
|
||
const { | ||
getColumnFormat, | ||
getColumnFormatMapForType, | ||
getColumnTypeFormatter, | ||
getFormattedString, | ||
} = formatter; | ||
|
||
return { | ||
getColumnFormat, | ||
getColumnFormatMapForType, | ||
getColumnTypeFormatter, | ||
getFormattedString, | ||
timeZone: formatter.timeZone, | ||
}; | ||
} | ||
|
||
export default useFormatter; |
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
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
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
Oops, something went wrong.