Skip to content

Commit

Permalink
test: Convert 'explain' tests to inline snapshots, for easier mainten…
Browse files Browse the repository at this point in the history
…ance.
  • Loading branch information
claremacrae committed Dec 26, 2023
1 parent e3d0ce3 commit df7a449
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 83 deletions.
112 changes: 59 additions & 53 deletions tests/Query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1196,99 +1196,99 @@ describe('Query', () => {
it('should explain 0 filters', () => {
const source = '';
const query = new Query(source);

const expectedDisplayText = 'No filters supplied. All tasks will match the query.';
expect(query.explainQuery()).toEqual(expectedDisplayText);
expect(query.explainQuery()).toMatchInlineSnapshot(
'"No filters supplied. All tasks will match the query."',
);
});

it('should explain 1 filter', () => {
const source = 'description includes hello';
const query = new Query(source);

const expectedDisplayText = `description includes hello
`;
expect(query.explainQuery()).toEqual(expectedDisplayText);
expect(query.explainQuery()).toMatchInlineSnapshot(`
"description includes hello
"
`);
});

it('should explain 2 filters', () => {
const source = 'description includes hello\ndue 2012-01-23';
const query = new Query(source);
expect(query.explainQuery()).toMatchInlineSnapshot(`
"description includes hello
const expectedDisplayText = `description includes hello
due 2012-01-23 =>
due date is on 2012-01-23 (Monday 23rd January 2012)
`;
expect(query.explainQuery()).toEqual(expectedDisplayText);
due 2012-01-23 =>
due date is on 2012-01-23 (Monday 23rd January 2012)
"
`);
});

it('should include any error message in the explanation', () => {
const source = 'i am a nonsense query';
const query = new Query(source);

const expectedDisplayText = `Query has an error:
do not understand query
Problem line: "i am a nonsense query"
`;
expect(query.explainQuery()).toEqual(expectedDisplayText);
expect(query.explainQuery()).toMatchInlineSnapshot(`
"Query has an error:
do not understand query
Problem line: "i am a nonsense query"
"
`);
});

it('should explain limit 5', () => {
const source = 'limit 5';
const query = new Query(source);
expect(query.explainQuery()).toMatchInlineSnapshot(`
"No filters supplied. All tasks will match the query.
const expectedDisplayText = `No filters supplied. All tasks will match the query.
At most 5 tasks.
`;
expect(query.explainQuery()).toEqual(expectedDisplayText);
At most 5 tasks.
"
`);
});

it('should explain limit 1', () => {
const source = 'limit 1';
const query = new Query(source);
expect(query.explainQuery()).toMatchInlineSnapshot(`
"No filters supplied. All tasks will match the query.
const expectedDisplayText = `No filters supplied. All tasks will match the query.
At most 1 task.
`;
expect(query.explainQuery()).toEqual(expectedDisplayText);
At most 1 task.
"
`);
});

it('should explain limit 0', () => {
const source = 'limit 0';
const query = new Query(source);
expect(query.explainQuery()).toMatchInlineSnapshot(`
"No filters supplied. All tasks will match the query.
const expectedDisplayText = `No filters supplied. All tasks will match the query.
At most 0 tasks.
`;
expect(query.explainQuery()).toEqual(expectedDisplayText);
At most 0 tasks.
"
`);
});

it('should explain group limit 4', () => {
const source = 'limit groups 4';
const query = new Query(source);
expect(query.explainQuery()).toMatchInlineSnapshot(`
"No filters supplied. All tasks will match the query.
const expectedDisplayText = `No filters supplied. All tasks will match the query.
At most 4 tasks per group (if any "group by" options are supplied).
`;
expect(query.explainQuery()).toEqual(expectedDisplayText);
At most 4 tasks per group (if any "group by" options are supplied).
"
`);
});

it('should explain all limit options', () => {
const source = 'limit 127\nlimit groups to 8 tasks';
const query = new Query(source);
expect(query.explainQuery()).toMatchInlineSnapshot(`
"No filters supplied. All tasks will match the query.
const expectedDisplayText = `No filters supplied. All tasks will match the query.
At most 127 tasks.
At most 127 tasks.
At most 8 tasks per group (if any "group by" options are supplied).
`;
expect(query.explainQuery()).toEqual(expectedDisplayText);
At most 8 tasks per group (if any "group by" options are supplied).
"
`);
});
});

Expand Down Expand Up @@ -1521,14 +1521,20 @@ At most 8 tasks per group (if any "group by" options are supplied).
with \ backslash)`;
const query = new Query(source);
const queryUpperCase = new Query(source);

const expectedDisplayText = String.raw`(description includes line 1) OR (description includes line 1 continued with \ backslash) =>
OR (At least one of):
description includes line 1
description includes line 1 continued with \ backslash
`;
expect(query.explainQuery()).toEqual(expectedDisplayText);
expect(queryUpperCase.explainQuery()).toEqual(expectedDisplayText);
expect(query.explainQuery()).toMatchInlineSnapshot(`
"(description includes line 1) OR (description includes line 1 continued with \\ backslash) =>
OR (At least one of):
description includes line 1
description includes line 1 continued with \\ backslash
"
`);
expect(queryUpperCase.explainQuery()).toMatchInlineSnapshot(`
"(description includes line 1) OR (description includes line 1 continued with \\ backslash) =>
OR (At least one of):
description includes line 1
description includes line 1 continued with \\ backslash
"
`);
});
});
});
56 changes: 26 additions & 30 deletions tests/lib/QueryRendererHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ describe('explain', () => {
it('should explain a task', () => {
const source = '';
const query = new Query(source);
expect(explainResults(query.source, new GlobalFilter(), new GlobalQuery())).toMatchInlineSnapshot(`
"Explanation of this Tasks code block query:
const expectedDisplayText = `Explanation of this Tasks code block query:
No filters supplied. All tasks will match the query.`;

expect(explainResults(query.source, new GlobalFilter(), new GlobalQuery())).toEqual(expectedDisplayText);
No filters supplied. All tasks will match the query."
`);
});

it('should explain a task with global filter active', () => {
Expand All @@ -27,30 +26,29 @@ No filters supplied. All tasks will match the query.`;

const source = '';
const query = new Query(source);
expect(explainResults(query.source, globalFilter, new GlobalQuery())).toMatchInlineSnapshot(`
"Only tasks containing the global filter '#task'.
const expectedDisplayText = `Only tasks containing the global filter '#task'.
Explanation of this Tasks code block query:
Explanation of this Tasks code block query:
No filters supplied. All tasks will match the query.`;
expect(explainResults(query.source, globalFilter, new GlobalQuery())).toEqual(expectedDisplayText);
No filters supplied. All tasks will match the query."
`);
});

it('should explain a task with global query active', () => {
const globalQuery = new GlobalQuery('description includes hello');

const source = '';
const query = new Query(source);
expect(explainResults(query.source, new GlobalFilter(), globalQuery)).toMatchInlineSnapshot(`
"Explanation of the global query:
const expectedDisplayText = `Explanation of the global query:
description includes hello
Explanation of this Tasks code block query:
description includes hello
No filters supplied. All tasks will match the query.`;
Explanation of this Tasks code block query:
expect(explainResults(query.source, new GlobalFilter(), globalQuery)).toEqual(expectedDisplayText);
No filters supplied. All tasks will match the query."
`);
});

it('should explain a task with global query and global filter active', () => {
Expand All @@ -60,31 +58,29 @@ No filters supplied. All tasks will match the query.`;

const source = '';
const query = new Query(source);
expect(explainResults(query.source, globalFilter, globalQuery)).toMatchInlineSnapshot(`
"Only tasks containing the global filter '#task'.
const expectedDisplayText = `Only tasks containing the global filter '#task'.
Explanation of the global query:
Explanation of the global query:
description includes hello
description includes hello
Explanation of this Tasks code block query:
Explanation of this Tasks code block query:
No filters supplied. All tasks will match the query.`;

expect(explainResults(query.source, globalFilter, globalQuery)).toEqual(expectedDisplayText);
No filters supplied. All tasks will match the query."
`);
});

it('should explain a task with global query set but ignored without the global query', () => {
const globalQuery = new GlobalQuery('description includes hello');

const source = 'ignore global query';
const query = new Query(source);
expect(explainResults(query.source, new GlobalFilter(), globalQuery)).toMatchInlineSnapshot(`
"Explanation of this Tasks code block query:
const expectedDisplayText = `Explanation of this Tasks code block query:
No filters supplied. All tasks will match the query.`;

expect(explainResults(query.source, new GlobalFilter(), globalQuery)).toEqual(expectedDisplayText);
No filters supplied. All tasks will match the query."
`);
});
});

Expand Down

0 comments on commit df7a449

Please sign in to comment.