Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

fix: (replace-use-search-params) do not create the hook file if it was created (INT-2089) #425

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions next/13/replace-use-search-params/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,25 +255,38 @@ export const repomod: Filemod<Dependencies, State> = {

if (state !== null && !state.hookCreated) {
if (state.hookPathType === 'relative') {
commands.push({
kind: 'upsertFile',
path: join(api.currentWorkingDirectory, state.hookPath),
options: {
...options,
fileContent: USE_COMPAT_SEARCH_PARAMS_HOOK_CONTENT,
},
});
const hookPath = join(
api.currentWorkingDirectory,
state.hookPath,
);

const hookPathExists = api.exists(hookPath);

if (!hookPathExists) {
commands.push({
kind: 'upsertFile',
path: hookPath,
options: {
...options,
fileContent: USE_COMPAT_SEARCH_PARAMS_HOOK_CONTENT,
},
});
}

state.hookCreated = true;
} else if (state.hookPathType === 'absolute') {
commands.push({
kind: 'upsertFile',
path: state.hookPath,
options: {
...options,
fileContent: USE_COMPAT_SEARCH_PARAMS_HOOK_CONTENT,
},
});
const hookPathExists = api.exists(state.hookPath);

if (!hookPathExists) {
commands.push({
kind: 'upsertFile',
path: state.hookPath,
options: {
...options,
fileContent: USE_COMPAT_SEARCH_PARAMS_HOOK_CONTENT,
},
});
}

state.hookCreated = true;
}
Expand Down
39 changes: 39 additions & 0 deletions next/13/replace-use-search-params/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,43 @@ describe('next 13 replace-replace-use-search-params', function () {
expectedResult.replace(/\s/gm, ''),
);
});

it('should replace useSearchParams with useCompatSearchParams without creating the hook file', async function (this: Context) {
const A_CONTENT = `
import { useSearchParams, useParams } from 'next/navigation';

export default function C() {
const s = useSearchParams();

return null;
}
`;

const [upsertFileCommand] = await transform({
'/opt/project/components/a.tsx': A_CONTENT,
'/opt/project/hooks/useCompatSearchParams.tsx': '',
});

const expectedResult = `
import { useCompatSearchParams } from "hooks/useCompatSearchParams.tsx";
import { useParams } from 'next/navigation';

export default function C() {
const s = useCompatSearchParams();

return null;
}
`;

deepStrictEqual(upsertFileCommand?.kind, 'upsertFile');
deepStrictEqual(
upsertFileCommand.path,
'/opt/project/components/a.tsx',
);

deepStrictEqual(
upsertFileCommand.data.replace(/\s/gm, ''),
expectedResult.replace(/\s/gm, ''),
);
});
});
Loading