123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import type {Location} from 'history';
- import {reactHooks} from 'sentry-test/reactTestingLibrary';
- import {decodeInteger, decodeList, decodeScalar} from 'sentry/utils/queryString';
- import useLocationQuery from 'sentry/utils/url/useLocationQuery';
- import {useLocation} from 'sentry/utils/useLocation';
- jest.mock('sentry/utils/useLocation');
- const mockLocation: Location = {
- key: '',
- search: '',
- hash: '',
- action: 'PUSH',
- state: null,
- query: {},
- pathname: '/mock-pathname/',
- };
- describe('useLocationQuery', () => {
- it('should read query values from the url', () => {
- jest.mocked(useLocation).mockReturnValue({
- ...mockLocation,
- query: {
- name: 'Adam',
- age: '12',
- titles: ['Mr.', 'Dr.'],
- extra: 'foo bar',
- },
- } as Location);
- const {result} = reactHooks.renderHook(useLocationQuery, {
- initialProps: {
- fields: {
- name: decodeScalar,
- age: decodeInteger,
- titles: decodeList,
- },
- },
- });
- expect(result.current).toStrictEqual({
- name: 'Adam',
- age: 12,
- titles: ['Mr.', 'Dr.'],
- });
- });
- it('should return undefined if the url does not contain a requested field', () => {
- jest.mocked(useLocation).mockReturnValue({
- ...mockLocation,
- query: {},
- } as Location);
- const {result} = reactHooks.renderHook(useLocationQuery, {
- initialProps: {
- fields: {
- name: decodeScalar,
- age: decodeInteger,
- titles: decodeList,
- },
- },
- });
- expect(result.current).toStrictEqual({
- name: '',
- age: 0,
- titles: [],
- });
- });
- it('should pass-through static values along with decoded ones', () => {
- jest.mocked(useLocation).mockReturnValueOnce({
- ...mockLocation,
- query: {
- name: 'Adam',
- titles: ['Mr.', 'Dr.'],
- },
- } as Location);
- const {result} = reactHooks.renderHook(useLocationQuery, {
- initialProps: {
- fields: {
- name: decodeScalar,
- stringy: 'bar',
- list: ['biz', 'baz'],
- num: 12,
- },
- },
- });
- expect(result.current).toStrictEqual({
- name: 'Adam',
- stringy: 'bar',
- list: ['biz', 'baz'],
- num: 12,
- });
- });
- it('should only change return object identity when values change', () => {
- // 1st render:
- jest.mocked(useLocation).mockReturnValueOnce({
- ...mockLocation,
- query: {
- name: 'Adam',
- titles: ['Mr.', 'Dr.'],
- },
- } as Location);
- // 2nd render, same values (but the array is re-built, new object ref):
- jest.mocked(useLocation).mockReturnValueOnce({
- ...mockLocation,
- query: {
- name: 'Adam',
- titles: ['Mr.', 'Dr.'],
- },
- } as Location);
- // 3rd render, name is changed.
- jest.mocked(useLocation).mockReturnValueOnce({
- ...mockLocation,
- query: {
- name: 'Betty',
- },
- } as Location);
- const {result, rerender} = reactHooks.renderHook(useLocationQuery, {
- initialProps: {
- fields: {
- name: decodeScalar,
- age: decodeInteger,
- titles: decodeList,
- },
- },
- });
- const first = result.current;
- rerender();
- const second = result.current;
- rerender();
- const third = result.current;
- expect(first.name).toBe('Adam');
- expect(second.name).toBe('Adam');
- expect(third.name).toBe('Betty');
- // Must be strict object equality:
- expect(first).toBe(second);
- // Object has changed because a value has changed:
- expect(first).not.toBe(third);
- });
- });
|