@@ -35,45 +35,66 @@ describe('mapColumn', () => {
3535 expect ( result . rows [ arbitraryRowIndex ] ) . toHaveProperty ( 'pricePlusTwo' ) ;
3636 } ) ;
3737
38- it ( 'matches name to id when mapColumn is called without an id' , async ( ) => {
39- const result = await runFn ( testTable , { name : 'name' , expression : pricePlusTwo } ) ;
40- const nameColumnIndex = result . columns . findIndex ( ( { name } ) => name === 'name' ) ;
41- const arbitraryRowIndex = 4 ;
42-
43- expect ( result . type ) . toBe ( 'datatable' ) ;
44- expect ( result . columns ) . toHaveLength ( sqlTable . columns . length ) ;
45- expect ( result . columns [ nameColumnIndex ] ) . toHaveProperty ( 'name' , 'name' ) ;
46- expect ( result . columns [ nameColumnIndex ] . meta ) . toHaveProperty ( 'type' , 'number' ) ;
47- expect ( result . rows [ arbitraryRowIndex ] ) . toHaveProperty ( 'name' , 202 ) ;
48- } ) ;
49-
50- it ( 'overwrites existing column with the new column if an existing column name is missing an id' , async ( ) => {
51- const result = await runFn ( sqlTable , { name : 'name' , expression : pricePlusTwo } ) ;
52- const nameColumnIndex = result . columns . findIndex ( ( { name } ) => name === 'name' ) ;
53- const arbitraryRowIndex = 4 ;
38+ describe ( 'when the table columns have id' , ( ) => {
39+ it ( 'does not require the id arg by using the name arg as column id' , async ( ) => {
40+ const result = await runFn ( testTable , { name : 'name' , expression : pricePlusTwo } ) ;
41+ const nameColumnIndex = result . columns . findIndex ( ( { name } ) => name === 'name' ) ;
42+ const arbitraryRowIndex = 4 ;
43+
44+ expect ( result . type ) . toBe ( 'datatable' ) ;
45+ expect ( result . columns ) . toHaveLength ( sqlTable . columns . length ) ;
46+ expect ( result . columns [ nameColumnIndex ] ) . toHaveProperty ( 'name' , 'name' ) ;
47+ expect ( result . columns [ nameColumnIndex ] . meta ) . toHaveProperty ( 'type' , 'number' ) ;
48+ expect ( result . rows [ arbitraryRowIndex ] ) . toHaveProperty ( 'name' , 202 ) ;
49+ } ) ;
5450
55- expect ( result . type ) . toBe ( 'datatable' ) ;
56- expect ( result . columns ) . toHaveLength ( sqlTable . columns . length ) ;
57- expect ( result . columns [ nameColumnIndex ] ) . toHaveProperty ( 'name' , 'name' ) ;
58- expect ( result . columns [ nameColumnIndex ] . meta ) . toHaveProperty ( 'type' , 'number' ) ;
59- expect ( result . rows [ arbitraryRowIndex ] ) . toHaveProperty ( 'name' , 202 ) ;
51+ it ( 'allows a duplicate name when the ids are different' , async ( ) => {
52+ const result = await runFn ( testTable , {
53+ id : 'new' ,
54+ name : 'name label' ,
55+ expression : pricePlusTwo ,
56+ } ) ;
57+ const nameColumnIndex = result . columns . findIndex ( ( { id } ) => id === 'new' ) ;
58+ const arbitraryRowIndex = 4 ;
59+
60+ expect ( result . type ) . toBe ( 'datatable' ) ;
61+ expect ( result . columns ) . toHaveLength ( testTable . columns . length + 1 ) ;
62+ expect ( result . columns [ nameColumnIndex ] ) . toHaveProperty ( 'id' , 'new' ) ;
63+ expect ( result . columns [ nameColumnIndex ] ) . toHaveProperty ( 'name' , 'name label' ) ;
64+ expect ( result . columns [ nameColumnIndex ] . meta ) . toHaveProperty ( 'type' , 'number' ) ;
65+ expect ( result . rows [ arbitraryRowIndex ] ) . toHaveProperty ( 'new' , 202 ) ;
66+ } ) ;
6067 } ) ;
6168
62- it ( 'inserts a new column with a duplicate name if an id and name are provided' , async ( ) => {
63- const result = await runFn ( testTable , {
64- id : 'new' ,
65- name : 'name label' ,
66- expression : pricePlusTwo ,
69+ describe ( 'when the table columns do not have id' , ( ) => {
70+ it ( 'uses name as unique key when id arg is also missing' , async ( ) => {
71+ const result = await runFn ( sqlTable , { name : 'name' , expression : pricePlusTwo } ) ;
72+ const nameColumnIndex = result . columns . findIndex ( ( { name } ) => name === 'name' ) ;
73+ const arbitraryRowIndex = 4 ;
74+
75+ expect ( result . type ) . toBe ( 'datatable' ) ;
76+ expect ( result . columns ) . toHaveLength ( sqlTable . columns . length ) ;
77+ expect ( result . columns [ nameColumnIndex ] ) . toHaveProperty ( 'name' , 'name' ) ;
78+ expect ( result . columns [ nameColumnIndex ] . meta ) . toHaveProperty ( 'type' , 'number' ) ;
79+ expect ( result . rows [ arbitraryRowIndex ] ) . toHaveProperty ( 'name' , 202 ) ;
6780 } ) ;
68- const nameColumnIndex = result . columns . findIndex ( ( { id } ) => id === 'new' ) ;
69- const arbitraryRowIndex = 4 ;
7081
71- expect ( result . type ) . toBe ( 'datatable' ) ;
72- expect ( result . columns ) . toHaveLength ( testTable . columns . length + 1 ) ;
73- expect ( result . columns [ nameColumnIndex ] ) . toHaveProperty ( 'id' , 'new' ) ;
74- expect ( result . columns [ nameColumnIndex ] ) . toHaveProperty ( 'name' , 'name label' ) ;
75- expect ( result . columns [ nameColumnIndex ] . meta ) . toHaveProperty ( 'type' , 'number' ) ;
76- expect ( result . rows [ arbitraryRowIndex ] ) . toHaveProperty ( 'new' , 202 ) ;
82+ it ( 'overwrites columns matching id === name when the column is missing an id' , async ( ) => {
83+ const result = await runFn ( sqlTable , {
84+ id : 'name' ,
85+ name : 'name is ignored' ,
86+ expression : pricePlusTwo ,
87+ } ) ;
88+ const nameColumnIndex = result . columns . findIndex ( ( { name } ) => name === 'name is ignored' ) ;
89+ const arbitraryRowIndex = 4 ;
90+
91+ expect ( result . type ) . toBe ( 'datatable' ) ;
92+ expect ( result . columns ) . toHaveLength ( sqlTable . columns . length ) ;
93+ expect ( result . columns [ nameColumnIndex ] ) . toHaveProperty ( 'id' , 'name' ) ;
94+ expect ( result . columns [ nameColumnIndex ] ) . toHaveProperty ( 'name' , 'name is ignored' ) ;
95+ expect ( result . columns [ nameColumnIndex ] . meta ) . toHaveProperty ( 'type' , 'number' ) ;
96+ expect ( result . rows [ arbitraryRowIndex ] ) . toHaveProperty ( 'name' , 202 ) ;
97+ } ) ;
7798 } ) ;
7899
79100 it ( 'adds a column to empty tables' , async ( ) => {
0 commit comments