Fixes https://github.com/twentyhq/core-team-issues/issues/950 This issue was due to the memoization inside `useIsMatchingLocation`, which was rerendered only if the pathname changed but not the search params. After discussion with @lucasbordeau, we decided to remove the hook `useIsMatchingLocation` and to create an equivalent util function which takes the location as an argument. --------- Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
90 lines
2.1 KiB
TypeScript
90 lines
2.1 KiB
TypeScript
import { AppBasePath } from '@/types/AppBasePath';
|
|
import { isMatchingLocation } from '../isMatchingLocation';
|
|
|
|
describe('isMatchingLocation', () => {
|
|
it('returns true when paths match with no basePath', () => {
|
|
const location = {
|
|
pathname: '/example',
|
|
state: null,
|
|
key: 'test-key',
|
|
search: '',
|
|
hash: '',
|
|
};
|
|
const result = isMatchingLocation(location, '/example');
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it('returns false when paths do not match with no basePath', () => {
|
|
const location = {
|
|
pathname: '/example',
|
|
state: null,
|
|
key: 'test-key',
|
|
search: '',
|
|
hash: '',
|
|
};
|
|
const result = isMatchingLocation(location, '/non-match');
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it('returns true when paths match with basePath', () => {
|
|
const location = {
|
|
pathname: `${AppBasePath.Settings}/example`,
|
|
state: null,
|
|
key: 'test-key',
|
|
search: '',
|
|
hash: '',
|
|
};
|
|
const result = isMatchingLocation(
|
|
location,
|
|
'example',
|
|
AppBasePath.Settings,
|
|
);
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it('returns false when paths do not match with basePath', () => {
|
|
const location = {
|
|
pathname: `${AppBasePath.Settings}/example`,
|
|
state: null,
|
|
key: 'test-key',
|
|
search: '',
|
|
hash: '',
|
|
};
|
|
|
|
const result = isMatchingLocation(
|
|
location,
|
|
'non-match',
|
|
AppBasePath.Settings,
|
|
);
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it('handles trailing slashes in basePath correctly', () => {
|
|
const location = {
|
|
pathname: `${AppBasePath.Settings}/example`,
|
|
state: null,
|
|
key: 'test-key',
|
|
search: '',
|
|
hash: '',
|
|
};
|
|
const result = isMatchingLocation(
|
|
location,
|
|
'example',
|
|
(AppBasePath.Settings + '/') as AppBasePath,
|
|
);
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it('handles paths without basePath correctly', () => {
|
|
const location = {
|
|
pathname: '/example',
|
|
state: null,
|
|
key: 'test-key',
|
|
search: '',
|
|
hash: '',
|
|
};
|
|
const result = isMatchingLocation(location, '/example');
|
|
expect(result).toBe(true);
|
|
});
|
|
});
|