feat(Codebytes): add tests for codebytes package disc 399#21
feat(Codebytes): add tests for codebytes package disc 399#21
Conversation
…tests-for-codebytes-package-disc-399
| hideCopyButton?: boolean; | ||
| onCopy?: CodebytesChangeHandler; | ||
| isIFrame?: boolean; | ||
| isForums?: boolean; |
There was a problem hiding this comment.
renaming this prop to just mention that the behavior is really specific to use in forums. figured it's better than 'isIFrame'
There was a problem hiding this comment.
If this becomes open source then we will want it to be iFrame again, right? Is there a plan for this to become open source?
There was a problem hiding this comment.
yeah we also may want to use codebytes on other non-discourse sites eventually! so +1 using a more generic name
There was a problem hiding this comment.
@j10sanders It is opensource as long at the client-modules repo is opensource. I'll change this to a more generic name, maybe isIFrame is better for now and if we decide to ever not use an iFrame in the plugin or on forums we can change!
There was a problem hiding this comment.
are you planning on changing the name in this pr?
There was a problem hiding this comment.
yes, i may have forgotten to do that here, let me update!
| .catch(() => console.error('Failed to copy')); | ||
| onCopy?.(text, language); | ||
| // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
| navigator.clipboard.writeText(text); |
There was a problem hiding this comment.
why are we removing the catch here?
There was a problem hiding this comment.
removed it, because i didn't think it was adding value, just logging an error to the console for the developer. can add it back if we really wish.
There was a problem hiding this comment.
the log isnt huge but we probably want to catch an error so it doesnt throw and break the page
There was a problem hiding this comment.
absolutely ok, can add back 👍
| }, []); | ||
| useEffect(() => { | ||
| if (language) { | ||
| setText(initialText ?? (helloWorld[language] || '')); |
There was a problem hiding this comment.
why are we adding this hook?
There was a problem hiding this comment.
From the unit tests in static-sites this intended behavior was made more clear: if the initial text is not provided, then the content is initialized with predefined text for the chosen language. The empty string at the end is for when there is not initial content for that language.
We need it in a useEffect, because if the text and language props change, the change needs to be reapplied.
There was a problem hiding this comment.
is there a way for language to change? i don't see a way for someone to do that, in which case there would just be the initial state
There was a problem hiding this comment.
@borisonr Hehe, good callout. Since the parent controls the language, and passes it in as props, the language can still technically change, though it's less likely to and more of a design question. This would make the component more flexible, for situations where we may want to pass in a different language from the parent, and what I feel may be be better for opensourcing it.
I can just add it as initial state, though, if we really like, but did not since technically can change.
There was a problem hiding this comment.
just trying to be mindful here that this pr was to add tests... probably shouldnt expand scope to potential future features?
There was a problem hiding this comment.
but query params would cause a page reload and still work with initial state, no?
There was a problem hiding this comment.
if we do want to do with something like initial state, we could do:
const getInitialText = () => {
if (initialText !== undefined) return initialText;
return initialLanguage ? helloWorld[initialLanguage] : '';
};
and then:
const [text, setText] = useState<string>(getInitialText());
this would also pass the original test
There was a problem hiding this comment.
ok with either, using the useEffect for extensibility with props being passed, but could also go with initial state if we want to keep it as state
There was a problem hiding this comment.
got it, this works! sorry for the back and forth was just trying to understand the non-test related changes in the pr!
There was a problem hiding this comment.
all good! I'll update this pr with this logic and clear out some merge conflicts!
| } | ||
| }} | ||
| snippetsBaseUrl={snippetsBaseUrl} | ||
| onCopy={onCopy} |
There was a problem hiding this comment.
did you mean to add oncopy here? was this related to a behavior we didn't realize until we added tests?
There was a problem hiding this comment.
@borisonr onCopy is intended to be here! we had it in the original code, and it may have gotten removed with the tracking updates, but we should see it trickle down to the Editor component as we had in previous prs.
when called in the Editor component, we can call it with the specific onCopy behavior that's needed for the iframe version or for the homepage version.
There was a problem hiding this comment.
@borisonr thank you for your ever-amazing reviews!
📬Published Alpha Packages:@codecademy/codebytes@0.5.1-alpha.a075a9.0 |
Overview
PR Checklist
Notes:
This pull request migrates tests for the Codebytes component. Please note any changes between the original and the tests in this pull request are described below:
Original tests in static-sites: https://github.com/codecademy-engineering/static-sites/tree/577de863974111b4021c2bec6f4b5d2b1684a3c8/src/templates/CodeByteEditor
See tests directory
codebytes-test.tsx
userEventin place offireEventand changed language select to follow userEvent syntax (examples of this are on line 81 ,93)ChangeHandlers. this tests the two callbacksonEditandonLanguagethat accept functions as props. tests ensure these are called with the appropriate data.isForumsprop istrueeditor-test.tsx
userEventin place offireEventChangeHandlers. tests ensuresonCopyis called with the appropriate data.language-selection-test.tsx
helpers-test.tsx
Other things:
yarn --focusworks to utilize the sibling package. i needed to add this command in codebytes' package.json, and also run it during the build process inconfig.yml.