1+ /**
2+ * @jest -environment jsdom
3+ */
4+
5+ import moment from 'moment' ;
16import { getSettings , resetSettings , updateSettings } from '../../src/Config/Settings' ;
27import { Query } from '../../src/Query/Query' ;
38import { TasksFile } from '../../src/Scripting/TasksFile' ;
49
10+ window . moment = moment ;
11+
12+ beforeEach ( ( ) => {
13+ jest . useFakeTimers ( ) ;
14+ jest . setSystemTime ( new Date ( '2025-04-28' ) ) ;
15+ } ) ;
16+
517afterEach ( ( ) => {
618 resetSettings ( ) ;
719} ) ;
@@ -17,6 +29,127 @@ describe('include tests', () => {
1729 expect ( query . filters . length ) . toEqual ( 1 ) ;
1830 expect ( query . filters [ 0 ] . statement . anyPlaceholdersExpanded ) . toEqual ( 'not done' ) ;
1931 } ) ;
32+
33+ it ( 'should accept whole-line include filter instruction' , ( ) => {
34+ updateSettings ( { includes : { not_done : 'not done' } } ) ;
35+
36+ const source = 'include not_done' ;
37+ const query = new Query ( source , new TasksFile ( 'stuff.md' ) ) ;
38+
39+ expect ( query . error ) . toBeUndefined ( ) ;
40+ expect ( query . source ) . toEqual ( 'include not_done' ) ;
41+ expect ( query . filters . length ) . toEqual ( 1 ) ;
42+ expect ( query . filters [ 0 ] . statement . anyPlaceholdersExpanded ) . toEqual ( 'not done' ) ;
43+ } ) ;
44+
45+ it ( 'should accept whole-line include layout instruction' , ( ) => {
46+ updateSettings ( { includes : { show_tree : 'show tree' } } ) ;
47+
48+ const source = 'include show_tree' ;
49+ const query = new Query ( source , new TasksFile ( 'stuff.md' ) ) ;
50+
51+ expect ( query . error ) . toBeUndefined ( ) ;
52+ expect ( query . source ) . toEqual ( 'include show_tree' ) ;
53+ expect ( query . queryLayoutOptions . hideTree ) . toEqual ( false ) ;
54+ expect ( query . layoutStatements [ 0 ] . anyPlaceholdersExpanded ) . toEqual ( 'show tree' ) ;
55+ } ) ;
56+
57+ it ( 'should accept multi-line include' , ( ) => {
58+ updateSettings ( { includes : { multi_line : 'scheduled tomorrow\nhide backlink' } } ) ;
59+
60+ const source = 'include multi_line' ;
61+ const query = new Query ( source , new TasksFile ( 'stuff.md' ) ) ;
62+
63+ expect ( query . error ) . toBeUndefined ( ) ;
64+
65+ expect ( query . filters . length ) . toEqual ( 1 ) ;
66+ expect ( query . filters [ 0 ] . statement . anyPlaceholdersExpanded ) . toEqual ( 'scheduled tomorrow' ) ;
67+
68+ expect ( query . queryLayoutOptions . hideBacklinks ) . toEqual ( true ) ;
69+ expect ( query . layoutStatements [ 0 ] . anyPlaceholdersExpanded ) . toEqual ( 'hide backlink' ) ;
70+ } ) ;
71+
72+ it ( 'should give a meaningful error for non-existent include' , ( ) => {
73+ updateSettings ( { includes : { } } ) ;
74+
75+ const source = 'include not_existent' ;
76+ const query = new Query ( source , new TasksFile ( 'stuff.md' ) ) ;
77+
78+ expect ( query . error ) . toMatchInlineSnapshot ( `
79+ "Cannot find include "not_existent" in the Tasks settings
80+ Problem line: "include not_existent""
81+ ` ) ;
82+ expect ( query . source ) . toEqual ( 'include not_existent' ) ;
83+ } ) ;
84+
85+ it ( 'should support nested include instructions' , ( ) => {
86+ updateSettings ( {
87+ includes : {
88+ inside : 'not done' ,
89+ out : 'include inside\nhide edit button' ,
90+ } ,
91+ } ) ;
92+
93+ const source = 'include out' ;
94+ const query = new Query ( source , new TasksFile ( 'stuff.md' ) ) ;
95+
96+ expect ( query . error ) . toBeUndefined ( ) ;
97+ expect ( query . source ) . toEqual ( 'include out' ) ;
98+
99+ expect ( query . filters . length ) . toEqual ( 1 ) ;
100+ expect ( query . filters [ 0 ] . statement . anyPlaceholdersExpanded ) . toEqual ( 'not done' ) ;
101+
102+ expect ( query . queryLayoutOptions . hideEditButton ) . toEqual ( true ) ;
103+ expect ( query . layoutStatements [ 0 ] . anyPlaceholdersExpanded ) . toEqual ( 'hide edit button' ) ;
104+ } ) ;
105+
106+ it ( 'should explain two levels of nested includes' , ( ) => {
107+ updateSettings ( {
108+ includes : {
109+ inside : '(happens this week) AND (starts before today)' ,
110+ out : 'include inside\nnot done' ,
111+ } ,
112+ } ) ;
113+
114+ const source = 'include out' ;
115+ const query = new Query ( source , new TasksFile ( 'stuff.md' ) ) ;
116+
117+ expect ( query . explainQuery ( ) ) . toMatchInlineSnapshot ( `
118+ "include out =>
119+ (happens this week) AND (starts before today) =>
120+ AND (All of):
121+ happens this week =>
122+ due, start or scheduled date is between:
123+ 2025-04-28 (Monday 28th April 2025) and
124+ 2025-05-04 (Sunday 4th May 2025) inclusive
125+ starts before today =>
126+ start date is before 2025-04-28 (Monday 28th April 2025) OR no start date
127+
128+ include out =>
129+ not done
130+ "
131+ ` ) ;
132+ } ) ;
133+
134+ it ( 'should give meaningful error message about included text' , ( ) => {
135+ updateSettings ( {
136+ includes : {
137+ inside : 'apple sauce' ,
138+ out : 'include inside' ,
139+ } ,
140+ } ) ;
141+
142+ const source = 'include out' ;
143+ const query = new Query ( source , new TasksFile ( 'stuff.md' ) ) ;
144+
145+ expect ( query . error ) . toMatchInlineSnapshot ( `
146+ "do not understand query
147+ Problem statement:
148+ include out =>
149+ apple sauce
150+ "
151+ ` ) ;
152+ } ) ;
20153} ) ;
21154
22155describe ( 'include settings tests' , ( ) => {
0 commit comments