Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type definitions for StyleOptions #3818

Merged
merged 21 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions __tests__/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ globals:
runHTMLTest: readonly
ignorePatterns:
- "**/*.js"
- "**/*.ts"
- "**/*.tsx"
- "!/html/__jest__/*.js"
- "!/setup/*.js"
2 changes: 1 addition & 1 deletion __tests__/adaptiveCards.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import scrollToBottomCompleted from './setup/conditions/scrollToBottomCompleted'
import uiConnected from './setup/conditions/uiConnected';

import createAdaptiveCardsHostConfig from '../packages/bundle/src/adaptiveCards/Styles/adaptiveCardHostConfig';
import defaultStyleOptions from '../packages/component/src/Styles/defaultStyleOptions';
import defaultStyleOptions from '../packages/api/src/defaultStyleOptions';

// selenium-webdriver API doc:
// https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_WebDriver.html
Expand Down
4 changes: 4 additions & 0 deletions __tests__/types/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dirNumber.js
dirString.js
styleOptionsAccent.js
styleOptionsCardEmph.js
10 changes: 10 additions & 0 deletions __tests__/types/dirNumber.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ReactWebChat, { createStyleSet, StyleOptions } from '../../packages/bundle/lib/index';

function main() {
const styleOptions: StyleOptions = { accent: 'black', cardEmphasisBackgroundColor: 'orange' };

createStyleSet(styleOptions);

// "dir" of type number should fail
return <ReactWebChat dir={123} styleOptions={styleOptions} />;
}
10 changes: 10 additions & 0 deletions __tests__/types/dirString.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ReactWebChat, { createStyleSet, StyleOptions } from '../../packages/bundle/lib/index';

function main() {
const styleOptions: StyleOptions = { accent: 'black', cardEmphasisBackgroundColor: 'orange' };

createStyleSet(styleOptions);

// Verify: "dir" of type string should pass
return <ReactWebChat dir="ltr" styleOptions={styleOptions} />;
}
10 changes: 10 additions & 0 deletions __tests__/types/styleOptionsAccent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ReactWebChat, { createStyleSet, StyleOptions } from '../../packages/bundle/lib/index-minimal';

function main() {
const styleOptions: StyleOptions = { accent: 'black' };

createStyleSet(styleOptions);

// Verify: Setting "accent" must should pass.
return <ReactWebChat dir="ltr" styleOptions={styleOptions} />;
}
10 changes: 10 additions & 0 deletions __tests__/types/styleOptionsCardEmph.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ReactWebChat, { createStyleSet, StyleOptions } from '../../packages/bundle/lib/index-minimal';

function main() {
const styleOptions: StyleOptions = { cardEmphasisBackgroundColor: 'orange' };

createStyleSet(styleOptions);

// Verify: Setting "cardEmphasisBackgroundColor" using minimal bundle must fail.
return <ReactWebChat dir="ltr" styleOptions={styleOptions} />;
}
54 changes: 54 additions & 0 deletions __tests__/types/typesCheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const ts = require('typescript');
const path = require('path');

function compile(...filenames) {
const program = ts.createProgram(filenames, {
corinagum marked this conversation as resolved.
Show resolved Hide resolved
allowSyntheticDefaultImports: true,
jsx: 'react',
noEmit: false,
skipLibCheck: true,
});

const emitResult = program.emit();
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
const errors = [];

allDiagnostics.forEach(diagnostic => {
if (diagnostic.file) {
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
errors.push(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
} else {
errors.push(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'));
}
});

if (errors.length > 0)

return errors;
};


it('dir should pass dir as string', () => {
corinagum marked this conversation as resolved.
Show resolved Hide resolved
const dirStringErrors = compile(path.join(__dirname, './dirString.tsx'));

expect(dirStringErrors).toHaveProperty('length', 0);
});

it('dir should fail on dir as number', () => {
const dirNumErrors = compile(path.join(__dirname, './dirNumber.tsx'));

expect(dirNumErrors).toHaveProperty('length', 1);
corinagum marked this conversation as resolved.
Show resolved Hide resolved
});

it('styleOptions should fail on accent', () => {
const dirStringErrors = compile(path.join(__dirname, './styleOptionsAccent.tsx'));

expect(dirStringErrors).toHaveProperty('length', 0);
});

it('styleOptions should pass on cardEmphasisBackgroundColor', () => {
const dirStringErrors = compile(path.join(__dirname, './styleOptionsCardEmph.tsx'));

expect(dirStringErrors).toHaveProperty('length', 1);
});
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"serve": "^11.3.2",
"serve-handler": "^6.1.3",
"strip-ansi": "^6.0.0",
"typescript": "^4.2.3",
"xmlbuilder": "^15.1.1"
},
"dependencies": {}
Expand Down
11 changes: 11 additions & 0 deletions packages/api/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ extends:
plugins:
- prettier
- react-hooks
- '@typescript-eslint/eslint-plugin'
parser:
'@typescript-eslint/parser'
rules:
# plugins
prettier/prettier: error
Expand Down Expand Up @@ -89,3 +92,11 @@ rules:
react/jsx-wrap-multilines: error # Conflict with no-extra-parens
react-hooks/rules-of-hooks: error
react-hooks/exhaustive-deps: warn

# TypeScript

# The correct way to use with typescript-eslint is to disable the core version.
# https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-use-before-define.md#how-to-use
no-use-before-define: off
'@typescript-eslint/no-use-before-define':
- error
Loading