Skip to content

Commit

Permalink
chore: Break extract spec into multiple parts
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Dec 25, 2024
1 parent e4ecc99 commit 8a29c5c
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions src/api/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,30 @@ interface RedSelMultipleObject {
}

describe('$.extract', () => {
it('() : should extract values for selectors', () => {
it('should return an empty object when no selectors are provided', () => {
const $ = load(fixtures.eleven);
const $root = $.root();

// An empty object should lead to an empty extraction.
expectTypeOf($root.extract({})).toEqualTypeOf<Record<never, never>>();
const emptyExtract = $root.extract({});
expect(emptyExtract).toStrictEqual({});
});

it('should return undefined for selectors that do not match any elements', () => {
const $ = load(fixtures.eleven);
const $root = $.root();

// Non-existent values should be undefined.
expectTypeOf($root.extract({ foo: 'bar' })).toEqualTypeOf<{
foo: string | undefined;
}>();
const simpleExtract = $root.extract({ foo: 'bar' });
expect(simpleExtract).toStrictEqual({ foo: undefined });
});

it('should extract values for existing selectors', () => {
const $ = load(fixtures.eleven);
const $root = $.root();

// Existing values should be extracted.
expectTypeOf($root.extract({ red: '.red' })).toEqualTypeOf<{
red: string | undefined;
}>();
Expand All @@ -42,8 +49,12 @@ describe('$.extract', () => {
red: 'Four',
sel: 'Three',
});
});

it('should extract values using descriptor objects', () => {
const $ = load(fixtures.eleven);
const $root = $.root();

// Descriptors for extractions should be supported.
expectTypeOf(
$root.extract({
red: { selector: '.red' },
Expand All @@ -56,8 +67,12 @@ describe('$.extract', () => {
sel: { selector: '.sel' },
}),
).toStrictEqual({ red: 'Four', sel: 'Three' });
});

it('should extract multiple values for selectors', () => {
const $ = load(fixtures.eleven);
const $root = $.root();

// Should support extraction of multiple values.
expectTypeOf(
$root.extract({
red: ['.red'],
Expand All @@ -73,8 +88,12 @@ describe('$.extract', () => {
red: ['Four', 'Five', 'Nine'],
sel: ['Three', 'Nine', 'Eleven'],
});
});

it('should extract custom properties specified by the user', () => {
const $ = load(fixtures.eleven);
const $root = $.root();

// Should support custom `prop`s.
expectTypeOf(
$root.extract({
red: { selector: '.red', value: 'outerHTML' },
Expand All @@ -87,8 +106,12 @@ describe('$.extract', () => {
sel: { selector: '.sel', value: 'tagName' },
}),
).toStrictEqual({ red: '<li class="red">Four</li>', sel: 'LI' });
});

it('should extract multiple custom properties for selectors', () => {
const $ = load(fixtures.eleven);
const $root = $.root();

// Should support custom `prop`s for multiple values.
expectTypeOf(
$root.extract({
red: [{ selector: '.red', value: 'outerHTML' }],
Expand All @@ -105,8 +128,12 @@ describe('$.extract', () => {
'<li class="red sel">Nine</li>',
],
});
});

it('should extract values using custom extraction functions', () => {
const $ = load(fixtures.eleven);
const $root = $.root();

// Should support custom extraction functions.
expectTypeOf(
$root.extract({
red: {
Expand All @@ -123,8 +150,12 @@ describe('$.extract', () => {
},
}),
).toStrictEqual({ red: 'red=Four' });
});

it('should extract multiple values using custom extraction functions', () => {
const $ = load(fixtures.eleven);
const $root = $.root();

// Should support custom extraction functions for multiple values.
expectTypeOf(
$root.extract({
red: [
Expand All @@ -145,8 +176,12 @@ describe('$.extract', () => {
],
}),
).toStrictEqual({ red: ['red=Four', 'red=Five', 'red=Nine'] });
});

it('should extract nested objects based on selectors', () => {
const $ = load(fixtures.eleven);
const $root = $.root();

// Should support extraction objects.
expectTypeOf(
$root.extract({
section: {
Expand Down Expand Up @@ -180,7 +215,7 @@ describe('$.extract', () => {
});
});

it('() : should not error on missing href prop (#4239)', () => {
it('should handle missing href properties without errors (#4239)', () => {
const $ = load(fixtures.eleven);
expect<{ links: string[] }>(
$.extract({ links: [{ selector: 'li', value: 'href' }] }),
Expand Down

0 comments on commit 8a29c5c

Please sign in to comment.