i have next.js application, with yarn. what is the best way to test it? how to add…
- User
i have next.js application, with yarn. what is the best way to test it? how to add tests, it's some kind of legacy i want to refactor, and there are no tests at all
- Assistant
- User
а как это вообще делается в принципе - тестирование next.js приложений? Опиши пожалуйста, я не очень понимаю, что вообще тестируется. ведь это плоноценный сайт, как его можно протестировать?
- Assistant
- User
ты говоришь проверка правильного рендеринга, но ведь это невозможно автоматизировать, т.е. надо генерить скриншоты и человек должен проверять, как это выглядит на экране?
- Assistant
- User
но ведь это очень сложно и сильно тормозит разработку
- Assistant
- User
const meta = item.meta_information_attributes;
48 │ const title = meta?.title || item.title;
47 │ const header = item.header;
46 │ const description = meta?.description || item.description;
45 │ const pathname = item?.path || router.asPath.split('?')[0];
44 │ const isMainPage = pathname === '/';
43 │ const canonicalUrl =${item.hostname}${isMainPage ? '' : pathname}; 42 │ const image = item.image ? : ''; - Assistant
Certainly! The code you're working with seems to be accessing properties of an object called
itemand its nestedmeta_information_attributes. While it seems to work fine, there are some improvements we can apply to enhance readability, avoid repetition, and clearly express the intent of each line. We can also simplify the handling of undefined values and optionally structure the meta attributes better. - User
const pathname = path || router.asPath.split('?')[0]; const isMainPage = pathname === '/'; const canonicalUrl =
${hostname}${isMainPage ? '' : pathname};i thought about this part, is it okay here?
- Assistant
- User
${item.hostname}${isMainPage ? '' : pathname};but is this okay? looks ugly
- Assistant
Your concern about the readability of the string interpolation is valid. When constructing URLs, especially when dealing with conditions, it can indeed become a bit cluttered. Improving readability is essential for maintaining code in the long run. Let’s enhance this line to make it more readable and understandable.
Possible Enhancements
-
Use a Conditional Statement: Instead of embedding the condition directly in the string interpolation, you can separate the logic out to enhance clarity.
-
Explicit Variable Naming: Use a well-named variable that makes it clear what you're constructing.
Here’s a refactored approach:
// Define canonical URL logic separately for clarity const pathSegment = isMainPage ? '' : pathname; const canonicalUrl = `${item.hostname}${pathSegment}`; -
- User
- Assistant
- User
so nothing globally changed, means code overall okay?
- Assistant
Yes, based on your provided code and the suggested refactorings, the core logic and functionality of your component seem solid. Your original implementation correctly pulls SEO-related data, constructs necessary meta tags, and utilizes React's
Headcomponent fromnext/headeffectively. - User
const threshold = (props.matter.platform.domain === 'udm-info.ru') ? 60 : 75;
┃ 2 │ │ │ │ │ │ │ if (uniqPercent < threshold) {
1 │ │ │ │ │ │ │ │ changeCheckboxWarn(
422 │ │ │ │ │ │ │ │ │ 'rss_yandex_zen', 1 │ │ │ │ │ │ │ │ │ false, ┃ 2 │ │ │ │ │ │ │ │ │ 'Уникальность меньше ' + threshold + '%, не постим в Дзен', 3 │ │ │ │ │ │ │ │ ); 4 │ │ │ │ │ │ │ } 5 │ │ │ │ │ │ │ - Assistant
Certainly! The provided code snippet checks the value of
uniqPercentagainst a threshold that varies depending on the domain of the platform. IfuniqPercentis less than the threshold, it calls a functionchangeCheckboxWarnto issue a warning.To improve readability and maintainability, we can:
- Destructure the relevant properties for clearer access to
props. - Use a constant or variable for the warning message instead of building it inline.
- Clarify the condition by encapsulating some of the logic.
Here's a refactored version of your code:
const { platform } = props.matter; // Destructuring for clarity const threshold = platform.domain === 'udm-info.ru' ? 60 : 75; if (uniqPercent < threshold) { const warningMessage = `Уникальность меньше ${threshold}%, не постим в Дзен`; changeCheckboxWarn('rss_yandex_zen', false, warningMessage); } - Destructure the relevant properties for clearer access to
- User
how in js console create props.matter.platform.domain = 'blabla'
- Assistant
To create or modify an object in the JavaScript console, you can directly assign values to properties. If you want to set
props.matter.platform.domainto'blabla', you'll need to ensure that the parent objects (props,matter, andplatform) are already defined. If they're not defined, you'll need to create them as well.Here's how you can do it in the console:
1. If
props,matter, andplatformare Already DefinedIf the
propsobject (and its nested properties) already exist in the console, you can simply assign the value like this: