Skip to content

feat(Codebytes): add tests for codebytes package disc 399#21

Merged
BandanaKM merged 99 commits intomainfrom
bm-add-tests-for-codebytes-package-disc-399
Jan 31, 2022
Merged

feat(Codebytes): add tests for codebytes package disc 399#21
BandanaKM merged 99 commits intomainfrom
bm-add-tests-for-codebytes-package-disc-399

Conversation

@BandanaKM
Copy link
Copy Markdown
Contributor

@BandanaKM BandanaKM commented Jan 22, 2022

Overview

PR Checklist

  • Related to JIRA ticket: DISC-399
  • I have run this code to verify it works
  • This PR includes unit tests for the code change

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

  1. codebytes-test.tsx

    • switched userEvent in place of fireEvent and changed language select to follow userEvent syntax (examples of this are on line 81 ,93)
    • added a describe block for ChangeHandlers. this tests the two callbacks onEdit and onLanguage that accept functions as props. tests ensure these are called with the appropriate data.
    • tracking tests are pretty much the same, except trackUserImpression is only called if the isForums prop is true
  2. editor-test.tsx

    • again, switched userEvent in place of fireEvent
    • again, a describe block for ChangeHandlers. tests ensures onCopy is called with the appropriate data.
  3. language-selection-test.tsx

    • identical. just checks for the updated language on the select now.
  4. helpers-test.tsx

  • removed BBCodeBlock test, since we're not doing that in this helper anymore. this BBCodeBlock helper will be in the next.js/forums version.

Other things:

  1. created a shared files for the mocks that are used both in the codebytes-test and editor-test
  2. codebytes also requires a sibling package (tracking) that also exists in this repo. running yarn --focus works to utilize the sibling package. i needed to add this command in codebytes' package.json, and also run it during the build process in config.yml.

Comment thread packages/codebytes/src/types.ts Outdated
hideCopyButton?: boolean;
onCopy?: CodebytesChangeHandler;
isIFrame?: boolean;
isForums?: boolean;
Copy link
Copy Markdown
Contributor Author

@BandanaKM BandanaKM Jan 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renaming this prop to just mention that the behavior is really specific to use in forums. figured it's better than 'isIFrame'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we also may want to use codebytes on other non-discourse sites eventually! so +1 using a more generic name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you planning on changing the name in this pr?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i may have forgotten to do that here, let me update!

@BandanaKM BandanaKM requested review from a team, dougyd92 and j10sanders and removed request for a team January 26, 2022 03:11
Comment thread packages/codebytes/src/editor.tsx Outdated
.catch(() => console.error('Failed to copy'));
onCopy?.(text, language);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
navigator.clipboard.writeText(text);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we removing the catch here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the log isnt huge but we probably want to catch an error so it doesnt throw and break the page

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absolutely ok, can add back 👍

Comment thread packages/codebytes/src/index.tsx Outdated
}, []);
useEffect(() => {
if (language) {
setText(initialText ?? (helloWorld[language] || ''));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we adding this hook?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

@borisonr borisonr Jan 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

@BandanaKM BandanaKM Jan 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just trying to be mindful here that this pr was to add tests... probably shouldnt expand scope to potential future features?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but query params would cause a page reload and still work with initial state, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, this works! sorry for the back and forth was just trying to understand the non-test related changes in the pr!

Copy link
Copy Markdown
Contributor Author

@BandanaKM BandanaKM Jan 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all good! I'll update this pr with this logic and clear out some merge conflicts!

Base automatically changed from hr-bm-migrate-codebytes-tracking-disc-355 to main January 27, 2022 14:05
@zippyzow zippyzow requested a review from songstephen as a code owner January 27, 2022 14:05
@BandanaKM BandanaKM changed the title Bm add tests for codebytes package disc 399 Feature(Codebytes) Bm add tests for codebytes package disc 399 Jan 29, 2022
@BandanaKM BandanaKM changed the title Feature(Codebytes) Bm add tests for codebytes package disc 399 Feature(Codebytes): add tests for codebytes package disc 399 Jan 30, 2022
@BandanaKM BandanaKM changed the title Feature(Codebytes): add tests for codebytes package disc 399 feat(Codebytes): add tests for codebytes package disc 399 Jan 30, 2022
@BandanaKM BandanaKM requested a review from borisonr January 30, 2022 23:09
}
}}
snippetsBaseUrl={snippetsBaseUrl}
onCopy={onCopy}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean to add oncopy here? was this related to a behavior we didn't realize until we added tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

glad we caught it!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@borisonr thank you for your ever-amazing reviews!

@codecademydev
Copy link
Copy Markdown
Contributor

📬Published Alpha Packages:

@codecademy/codebytes@0.5.1-alpha.a075a9.0
@codecademy/styleguide@0.5.1-alpha.a075a9.0

@BandanaKM BandanaKM merged commit df3f780 into main Jan 31, 2022
@BandanaKM BandanaKM deleted the bm-add-tests-for-codebytes-package-disc-399 branch January 31, 2022 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants