From df7a449f88cf4d3a9a7911318b6914c71348ac27 Mon Sep 17 00:00:00 2001 From: Clare Macrae Date: Mon, 25 Dec 2023 22:47:10 +0000 Subject: [PATCH] test: Convert 'explain' tests to inline snapshots, for easier maintenance. --- tests/Query.test.ts | 112 ++++++++++++++------------ tests/lib/QueryRendererHelper.test.ts | 56 ++++++------- 2 files changed, 85 insertions(+), 83 deletions(-) diff --git a/tests/Query.test.ts b/tests/Query.test.ts index 87e8914537..865f5196f1 100644 --- a/tests/Query.test.ts +++ b/tests/Query.test.ts @@ -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). + " + `); }); }); @@ -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 + " + `); }); }); }); diff --git a/tests/lib/QueryRendererHelper.test.ts b/tests/lib/QueryRendererHelper.test.ts index ec419e145e..9569386b31 100644 --- a/tests/lib/QueryRendererHelper.test.ts +++ b/tests/lib/QueryRendererHelper.test.ts @@ -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', () => { @@ -27,13 +26,13 @@ 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', () => { @@ -41,16 +40,15 @@ No filters supplied. All tasks will match the query.`; 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', () => { @@ -60,18 +58,17 @@ 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', () => { @@ -79,12 +76,11 @@ No filters supplied. All tasks will match the query.`; 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." + `); }); });