Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dogusata committed Jun 27, 2024
1 parent 1423b9a commit eec24ec
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/components/__test__/button.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ describe('button', () => {
onClick: mockOnClickHandler,
});

expect(testButton.render.textContent).toBe('Test button');
expect(testButton.render).toBeDefined();
expect(testButton.render.querySelector('span')?.textContent?.trim()).toBe('Test button');

testButton.updateLabel('Updated label');
expect(testButton.render.textContent).toBe('Updated label');
expect(testButton.render.textContent?.trim()).toBe('Updated label');
});

it('attributes', () => {
Expand Down Expand Up @@ -66,7 +67,7 @@ describe('button', () => {
},
onClick: mockOnClickHandler,
additionalEvents: {
mouseover: mockMouseOverHandler,
mouseenter: mockMouseOverHandler,
}
});

Expand All @@ -75,7 +76,7 @@ describe('button', () => {
testButtonElement?.click();
expect(mockOnClickHandler).toHaveBeenCalledTimes(1);

testButtonElement.dispatchEvent(new Event('mouseover'));
testButtonElement.dispatchEvent(new Event('mouseenter'));
expect(mockMouseOverHandler).toHaveBeenCalledTimes(1);
});
});
4 changes: 2 additions & 2 deletions src/components/__test__/feedback-form/feedback-form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('feedback form', () => {
});

const submitButtonElement = testFeedbackForm.feedbackFormContainer.querySelectorAll('button')[2];
expect(submitButtonElement.textContent).toBe('Submit');
expect(submitButtonElement.textContent?.trim()).toBe('Submit');
submitButtonElement.click();
expect(spyDispatch).toHaveBeenCalledTimes(2);
expect(spyDispatch).toHaveBeenNthCalledWith(1, MynahEventNames.SHOW_FEEDBACK_FORM, {
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('feedback form', () => {
});

const cancelButtonElement = testFeedbackForm.feedbackFormContainer.querySelectorAll('button')[1];
expect(cancelButtonElement.textContent).toBe('Cancel');
expect(cancelButtonElement.textContent?.trim()).toBe('Cancel');
cancelButtonElement.click();
expect(spyDispatch).toHaveBeenCalledTimes(1);
expect(spyDispatch).toHaveBeenNthCalledWith(1, MynahEventNames.SHOW_FEEDBACK_FORM, {
Expand Down
10 changes: 4 additions & 6 deletions src/components/__test__/syntax-highlighter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ describe('syntax-highlighter', () => {
codeBlockActions: {
copy: {
id: 'copy',
label: 'Copy',
acceptedLanguages: [ 'typescript' ]
label: 'Copy'
},
'insert-at-cursor': {
id: 'insert-at-cursor',
label: 'Insert at cursor',
acceptedLanguages: [ 'typescript' ]
label: 'Insert at cursor'
},
},
onCopiedToClipboard: () => {},
onCodeBlockAction: () => {},
block: true,
});
expect(testSyntaxHighlighter.render.querySelectorAll('button')?.length).toBe(2);
expect(testSyntaxHighlighter.render.querySelectorAll('button')?.[0]?.textContent).toBe('Copy');
expect(testSyntaxHighlighter.render.querySelectorAll('button')?.[1]?.textContent).toBe('Insert at cursor');
expect(testSyntaxHighlighter.render.querySelectorAll('button')?.[0]?.textContent?.trim()).toBe('Copy');
expect(testSyntaxHighlighter.render.querySelectorAll('button')?.[1]?.textContent?.trim()).toBe('Insert at cursor');
});
});
21 changes: 14 additions & 7 deletions src/components/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { marked } from 'marked';
import { DomBuilder, ExtendedHTMLElement } from '../helper/dom';
import { DomBuilder, DomBuilderObject, ExtendedHTMLElement } from '../helper/dom';
import { Overlay, OverlayHorizontalDirection, OverlayVerticalDirection } from './overlay';
import { Card } from './card/card';
import { CardBody } from './card/card-body';
Expand Down Expand Up @@ -93,16 +93,23 @@ class ButtonInternal extends ButtonAbstract {
},
children: [
...(props.icon !== undefined ? [ props.icon ] : []),
...(props.label !== undefined
? typeof props.label !== 'string'
? [ { type: 'span', classNames: [ 'mynah-button-label' ], children: [ typeof props.label === 'string' ? marked(props.label) as string : props.label ] } ]
: [ { type: 'span', classNames: [ 'mynah-button-label' ], innerHTML: marked(escapeHTML(props.label)) as string } ]
: []),
...(this.getButtonLabelDomBuilderObject(props.label)),
...(props.children ?? []),
],
});
}

private readonly getButtonLabelDomBuilderObject = (label?: HTMLElement | ExtendedHTMLElement | string): DomBuilderObject[] => {
if (label !== undefined) {
if (typeof label !== 'string') {
return [ { type: 'span', classNames: [ 'mynah-button-label' ], children: [ typeof label === 'string' ? marked(label) as string : label ] } ];
} else {
return [ { type: 'span', classNames: [ 'mynah-button-label' ], innerHTML: marked(escapeHTML(label)) as string } ];
}
}
return [];
};

private readonly showTooltip = (content: string): void => {
if (content.trim() !== undefined) {
clearTimeout(this.tooltipTimeout);
Expand Down Expand Up @@ -141,7 +148,7 @@ class ButtonInternal extends ButtonAbstract {

public readonly updateLabel = (label: HTMLElement | ExtendedHTMLElement | string): void => {
(this.render.querySelector('.mynah-button-label') as ExtendedHTMLElement).replaceWith(
DomBuilder.getInstance().build({ type: 'span', classNames: [ 'mynah-button-label' ], children: [ label ] })
DomBuilder.getInstance().build(this.getButtonLabelDomBuilderObject(label)[0])
);
};

Expand Down

0 comments on commit eec24ec

Please sign in to comment.