|
| 1 | +/** |
| 2 | + * @fileoverview Tests for no-use-client-in-server-files rule |
| 3 | + */ |
| 4 | + |
| 5 | +const { RuleTester } = require('eslint'); |
| 6 | +const rule = require('./no-use-client-in-server-files.cjs'); |
| 7 | + |
| 8 | +const ruleTester = new RuleTester({ |
| 9 | + languageOptions: { |
| 10 | + ecmaVersion: 2022, |
| 11 | + sourceType: 'module', |
| 12 | + parserOptions: { |
| 13 | + ecmaFeatures: { |
| 14 | + jsx: true, |
| 15 | + }, |
| 16 | + }, |
| 17 | + }, |
| 18 | +}); |
| 19 | + |
| 20 | +ruleTester.run('no-use-client-in-server-files', rule, { |
| 21 | + valid: [ |
| 22 | + { |
| 23 | + code: ` |
| 24 | +import React from 'react'; |
| 25 | +
|
| 26 | +export function ServerComponent() { |
| 27 | + return <div>Server Component</div>; |
| 28 | +} |
| 29 | +`, |
| 30 | + filename: 'Component.server.tsx', |
| 31 | + }, |
| 32 | + { |
| 33 | + code: ` |
| 34 | +import { renderToString } from 'react-dom/server'; |
| 35 | +
|
| 36 | +export function render() { |
| 37 | + return renderToString(<div>Hello</div>); |
| 38 | +} |
| 39 | +`, |
| 40 | + filename: 'ComponentRenderer.server.tsx', |
| 41 | + }, |
| 42 | + { |
| 43 | + code: ` |
| 44 | +'use client'; |
| 45 | +
|
| 46 | +import React from 'react'; |
| 47 | +
|
| 48 | +export function ClientComponent() { |
| 49 | + return <div>Client Component</div>; |
| 50 | +} |
| 51 | +`, |
| 52 | + filename: 'Component.client.tsx', |
| 53 | + }, |
| 54 | + { |
| 55 | + code: ` |
| 56 | +'use client'; |
| 57 | +
|
| 58 | +import React from 'react'; |
| 59 | +
|
| 60 | +export function ClientComponent() { |
| 61 | + return <div>Client Component</div>; |
| 62 | +} |
| 63 | +`, |
| 64 | + filename: 'Component.tsx', |
| 65 | + }, |
| 66 | + { |
| 67 | + code: ` |
| 68 | +import React from 'react'; |
| 69 | +
|
| 70 | +// This is fine - no 'use client' directive |
| 71 | +export function ServerComponent() { |
| 72 | + return <div>Server</div>; |
| 73 | +} |
| 74 | +`, |
| 75 | + filename: 'App.server.ts', |
| 76 | + }, |
| 77 | + ], |
| 78 | + |
| 79 | + invalid: [ |
| 80 | + { |
| 81 | + code: `'use client'; |
| 82 | +
|
| 83 | +import React from 'react'; |
| 84 | +
|
| 85 | +export function Component() { |
| 86 | + return <div>Component</div>; |
| 87 | +} |
| 88 | +`, |
| 89 | + filename: 'Component.server.tsx', |
| 90 | + errors: [ |
| 91 | + { |
| 92 | + messageId: 'useClientInServerFile', |
| 93 | + }, |
| 94 | + ], |
| 95 | + output: `import React from 'react'; |
| 96 | +
|
| 97 | +export function Component() { |
| 98 | + return <div>Component</div>; |
| 99 | +} |
| 100 | +`, |
| 101 | + }, |
| 102 | + { |
| 103 | + code: `"use client"; |
| 104 | +
|
| 105 | +import React from 'react'; |
| 106 | +`, |
| 107 | + filename: 'Component.server.tsx', |
| 108 | + errors: [ |
| 109 | + { |
| 110 | + messageId: 'useClientInServerFile', |
| 111 | + }, |
| 112 | + ], |
| 113 | + output: `import React from 'react'; |
| 114 | +`, |
| 115 | + }, |
| 116 | + { |
| 117 | + code: `'use client' |
| 118 | +
|
| 119 | +import React from 'react'; |
| 120 | +`, |
| 121 | + filename: 'Component.server.tsx', |
| 122 | + errors: [ |
| 123 | + { |
| 124 | + messageId: 'useClientInServerFile', |
| 125 | + }, |
| 126 | + ], |
| 127 | + output: `import React from 'react'; |
| 128 | +`, |
| 129 | + }, |
| 130 | + { |
| 131 | + code: ` 'use client'; |
| 132 | +
|
| 133 | +import wrapServerComponentRenderer from 'react-on-rails-pro/wrapServerComponentRenderer/server'; |
| 134 | +`, |
| 135 | + filename: 'AsyncOnServerSyncOnClient.server.tsx', |
| 136 | + errors: [ |
| 137 | + { |
| 138 | + messageId: 'useClientInServerFile', |
| 139 | + }, |
| 140 | + ], |
| 141 | + output: `import wrapServerComponentRenderer from 'react-on-rails-pro/wrapServerComponentRenderer/server'; |
| 142 | +`, |
| 143 | + }, |
| 144 | + { |
| 145 | + code: `'use client'; |
| 146 | +
|
| 147 | +import React from 'react'; |
| 148 | +`, |
| 149 | + filename: 'Component.server.ts', |
| 150 | + errors: [ |
| 151 | + { |
| 152 | + messageId: 'useClientInServerFile', |
| 153 | + }, |
| 154 | + ], |
| 155 | + output: `import React from 'react'; |
| 156 | +`, |
| 157 | + }, |
| 158 | + ], |
| 159 | +}); |
| 160 | + |
| 161 | +console.log('All tests passed!'); |
0 commit comments