generate-react-cli 8.2.0 → 8.3.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generate-react-cli",
3
- "version": "8.2.0",
3
+ "version": "8.3.0-alpha.1",
4
4
  "description": "A simple React CLI to generate components instantly and more.",
5
5
  "repository": "https://github.com/arminbro/generate-react-cli",
6
6
  "bugs": "https://github.com/arminbro/generate-react-cli/issues",
package/readme.md CHANGED
@@ -478,6 +478,49 @@ GlobalNav.defaultProps = {};
478
478
  export default GlobalNav;
479
479
  ```
480
480
 
481
+ If you have unit testing enabled in your project and have the `withTest` flag enabled, it will generate the unit test file and initialize with some test cases for the generated component right out of the box!
482
+
483
+ Here are the unit tests it generated for the GlobalNav component using the default testing library template (`@testing-library/react`) that comes with GRC.
484
+
485
+ ```tsx
486
+ import React from 'react';
487
+ import { render, screen } from '@testing-library/react';
488
+ import '@testing-library/jest-dom/extend-expect';
489
+ import GlobalNav from './GlobalNav';
490
+
491
+ describe('<GlobalNav />', () => {
492
+ test('it should mount', () => {
493
+ render(<GlobalNav />);
494
+
495
+ const globalNav = screen.getByTestId('GlobalNav');
496
+
497
+ expect(globalNav).toBeInTheDocument();
498
+ });
499
+
500
+ test('it should display the logo "GRC"', () => {
501
+ render(<GlobalNav />);
502
+
503
+ const logo = screen.getByText('GRC');
504
+
505
+ expect(logo).toBeInTheDocument();
506
+ });
507
+
508
+ test('it should display three links: Home, About and Contact', () => {
509
+ render(<GlobalNav />);
510
+
511
+ const homeLink = screen.getByText('Home');
512
+ const aboutLink = screen.getByText('About');
513
+ const contactLink = screen.getByText('Contact');
514
+
515
+ expect(homeLink).toBeInTheDocument();
516
+ expect(aboutLink).toBeInTheDocument();
517
+ expect(contactLink).toBeInTheDocument();
518
+ });
519
+ });
520
+ ```
521
+
522
+ Please note, If you're using a different testing library, you need to provide it as a [custom testing component template](#custom-component-templates), and GRC will instruct openAi to use that to write your unit tests.
523
+
481
524
  That's a wrap. I hope this integration will allow us to generate React components more efficiently, and we can still go in and make the necessary adjustments.
482
525
 
483
526
  Again, please provide any feedback if you have any, and I would love to see some of the components that you generate with GRC+OpenAI.
@@ -16,3 +16,20 @@ export async function aiComponentGenerator(componentTemplate, prompt) {
16
16
 
17
17
  return generatedComponent.data.choices[0].text;
18
18
  }
19
+
20
+ export async function aiComponentTestGenerator(componentTemplate, prompt) {
21
+ const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY });
22
+ const openAiApi = new OpenAIApi(configuration);
23
+
24
+ const generatedComponent = await openAiApi.createCompletion({
25
+ model: 'text-davinci-003',
26
+ prompt: `Create a React component unit tests using this template "${componentTemplate}", but make the adjustments needed with these instructions as follows "${prompt}"`,
27
+ temperature: 0.7,
28
+ max_tokens: 2000,
29
+ top_p: 1.0,
30
+ frequency_penalty: 0.0,
31
+ presence_penalty: 1,
32
+ });
33
+
34
+ return generatedComponent.data.choices[0].text;
35
+ }
@@ -7,7 +7,7 @@ import snakeCase from 'lodash/snakeCase.js';
7
7
  import startCase from 'lodash/startCase.js';
8
8
  import fsExtra from 'fs-extra';
9
9
 
10
- import { aiComponentGenerator } from '../services/openAiService.js';
10
+ import { aiComponentGenerator, aiComponentTestGenerator } from '../services/openAiService.js';
11
11
  import componentJsTemplate from '../templates/component/componentJsTemplate.js';
12
12
  import componentTsTemplate from '../templates/component/componentTsTemplate.js';
13
13
  import componentCssTemplate from '../templates/component/componentCssTemplate.js';
@@ -438,6 +438,30 @@ export function generateComponent(componentName, cmd, cliConfigFile) {
438
438
  return;
439
439
  }
440
440
 
441
+ // Generate component test with openAi, if component description is provided
442
+
443
+ if (cmd.describe && componentFileType === buildInComponentFileTypes.TEST) {
444
+ aiComponentTestGenerator(template, cmd.describe)
445
+ .then((aiGeneratedComponentTest) => {
446
+ outputFileSync(componentPath, aiGeneratedComponentTest.trim());
447
+ console.log(
448
+ chalk.green(
449
+ `OpenAI Successfully created the ${filename} component test with the provided description.`
450
+ )
451
+ );
452
+ })
453
+ .catch((error) =>
454
+ console.log(
455
+ chalk.red(
456
+ `OpenAI failed to create the ${filename} component test with the provided description.`,
457
+ error
458
+ )
459
+ )
460
+ );
461
+
462
+ return;
463
+ }
464
+
441
465
  console.log(chalk.green(`${filename} was successfully created at ${componentPath}`));
442
466
  } catch (error) {
443
467
  console.error(chalk.red(`${filename} failed and was not created.`));