TILs - Fueling Curiosity, One Insight at a Time

At Codemancers, we believe every day is an opportunity to grow. This section is where our team shares bite-sized discoveries, technical breakthroughs and fascinating nuggets of wisdom we've stumbled upon in our work.

Published
Author
user-image
Syed Sibtain
System Analyst
So if we want to mock the useRouter hook in NextJs, we can use vi for it.
The "vi" library's function vi.mock allows us to simulate the behaviour of the useRouter hook. The mock function returns an object that mimics the Router object's properties and methods.


Code

vi.mock("next/router", () => ({
    useRouter: () => ({
      replace: vi.fn(),
    }),
  }));


replace: a mock function that simulates the behavior of the replace method of the Router object.
If we do not use it, the test might fail and we get the following error.
Error: NextRouter was not mounted.
Published
Author
user-image
Syed Sibtain
System Analyst
userEvent is considered to be a more reliable and secure way of triggering events in the browser because user events are events that are triggered by a user's interaction with the webpage and reflect the actual user behavior on the page.
However, fireEvent is a means of programmatically activating an event (dispatching DOM events) in the browser, which can be less dependable.

Reference: https://testing-library.com/docs/user-event/intro/#difference-to-fireevent
Published
Author
user-image
Satya
userEvent is more reliable and easy to use rather than fireEvent.
userEvent provides wide range of browser events options and it is like interacting with events same as we interact in browser
Published
Author
user-image
Vaibhav Yadav
Senior System Analyst
Postgres query uses || as string concatenation instead of +
Published
Author
user-image
Ashwani Kumar Jha
Senior System Analyst
We can type JSON into https://app.quicktype.io/ and it generates TypeScript type.

Can be helpful in defining types for api response (usually while integrating third party apis where large amounts of data is returned).
Published
Author
user-image
Keshav Chakravarthy
you can whitelist IPs and ranges in nginx. When we have separate virtual hosts these are saved in /etc/nginx/sites-enabled. The rules look like allow xx.xx.xx.xx/range(32); and deny all;. Further we can make sure nginx passes some headers to the app using the rules proxy_set_header Host $http_host;.
Published
Author
user-image
Vaibhav Yadav
Senior System Analyst
We can use the following where clause to filter users based on their age using a Postgres query
(CURRENT_DATE - INTERVAL '18 years')::date < TO_DATE(T3.DATE_OF_BIRTH, 'YYYY-MM-DD')
Published
Author
user-image
Ashwani Kumar Jha
Senior System Analyst
As a Next.js app can be executed on both the server and the client side. When the app is rendered on the server, there is no access to the browser-specific features like localStorage, as it is a feature of the client-side browser environment.

So let's say, if we try to use localStorage in a Next.js app, we may encounter issues when the app is being server-side rendered.

To avoid this problem, we can check if localStorage is available before using it in our Next.js app.


Code

if(typeoflocalStorage!== 'undefined') { 
// Use localStorage here
}


This way, our app will only use localStorage when it is available, and avoid errors when it is not.
Published
Author
user-image
Sujay
Bring the suspended server to foreground using fg command. Say the server is running and we want to install a package

Code

1. Ctrl+Z
2. yarn add jsonwebtoken
3. fg


And the server resumes

Showing 47 to 49 of 82 results

Ready to Build Something Amazing?

Codemancers can bring your vision to life and help you achieve your goals