Skip to content

Commit

Permalink
test(component-my-circle): simplified tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schürmann committed Dec 28, 2024
1 parent 82b2442 commit 6d995a2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
6 changes: 5 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ module.exports = [
document: 'readonly',
process: 'readonly',
console: 'readonly',
Document: 'readonly'
Document: 'readonly',
ShadowRoot: 'readonly',
SVGSVGElement: 'readonly',
SVGCircleElement: 'readonly',
Element: 'readonly'
}
},
plugins: {
Expand Down
33 changes: 14 additions & 19 deletions packages/component-my-circle/test/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,50 @@ const componentPath = resolve(__dirname, '../src/my-circle.ts');
describe('MyCircle Component', () => {
let mountContext: MountContext;

let circleTag: Element;
let shadowRoot: ShadowRoot;
let svg: SVGSVGElement;
let circleElement: SVGCircleElement;

beforeEach(async () => {
const helper = new TestHelper();
mountContext = await helper.compileAndMountAsScript('my-circle', componentPath);
const { document } = mountContext;
circleTag = document.querySelector('my-circle')!;
shadowRoot = circleTag?.shadowRoot as ShadowRoot;
svg = shadowRoot?.querySelector('svg') as SVGSVGElement;
circleElement = svg?.querySelector('circle') as SVGCircleElement;
});

afterEach(() => {
mountContext.jsdom.window.close();
});

it('should create a circle element', () => {
const { document } = mountContext;
const circle = document.querySelector('my-circle');
assert.ok(circle, 'Circle element should exist');
assert.strictEqual(circle?.tagName.toLowerCase(), 'my-circle');
assert.ok(circleTag, 'Circle element should exist');
assert.strictEqual(circleTag?.tagName.toLowerCase(), 'my-circle');
});

it('should create a shadow root', () => {
const { document } = mountContext;
const circle = document.querySelector('my-circle');
const shadowRoot = circle?.shadowRoot;
assert.ok(shadowRoot, 'Shadow root should exist');
});

it('should render an SVG circle', () => {
const { document } = mountContext;
const circle = document.querySelector('my-circle');
const shadowRoot = circle?.shadowRoot;
const svg = shadowRoot?.querySelector('svg');
const circleElement = svg?.querySelector('circle');

assert.ok(svg, 'SVG element should exist');
assert.ok(circleElement, 'Circle element should exist in SVG');
});

it('should update circle color when attribute changes', async () => {
const { document } = mountContext;
const circle = document.querySelector('my-circle');
const shadowRoot = circle?.shadowRoot;
const svgCircle = shadowRoot?.querySelector('circle');

// Default color
assert.strictEqual(
svgCircle?.getAttribute('fill'),
circleElement?.getAttribute('fill'),
'red',
'Default color should be red'
);

// Change color
circle?.setAttribute('color', 'blue');
circleTag?.setAttribute('color', 'blue');
await new Promise(resolve => setTimeout(resolve, 200));

const svgCircle2 = shadowRoot?.querySelector('circle');
Expand Down

0 comments on commit 6d995a2

Please sign in to comment.