-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from akvo/feature/39-cascade-question-type-test
Feature/39 cascade question type test
- Loading branch information
Showing
30 changed files
with
777 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module.exports = | ||
'CREATE TABLE examples( id INT PRIMARY KEY, name VARCHAR(100) );INSERT INTO examples(id, name) values(1, "JHON")'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const { getDefaultConfig } = require('expo/metro-config'); | ||
|
||
const defaultConfig = getDefaultConfig(__dirname); | ||
|
||
defaultConfig.resolver.assetExts.push('db'); | ||
|
||
module.exports = defaultConfig; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Asset } from 'expo-asset'; | ||
import * as FileSystem from 'expo-file-system'; | ||
import * as SQLite from 'expo-sqlite'; | ||
import { act, renderHook, waitFor, fireEvent } from '@testing-library/react-native'; | ||
import { conn } from '../conn'; | ||
import exampledb from 'assets/example.db'; | ||
|
||
jest.mock('expo-asset', () => { | ||
return { | ||
Asset: { | ||
fromModule: jest.fn((module) => ({ | ||
uri: `mocked-uri-for-${module}`, | ||
})), | ||
}, | ||
}; | ||
}); | ||
|
||
jest.mock('expo-file-system', () => { | ||
return { | ||
getInfoAsync: jest.fn().mockResolvedValue({ exists: false }), | ||
makeDirectoryAsync: jest.fn(), | ||
downloadAsync: jest.fn(), | ||
}; | ||
}); | ||
|
||
describe('openDBFile', () => { | ||
it('should have db connection from file', async () => { | ||
const dbFile = exampledb; | ||
const db = await conn.file(dbFile, 'example'); | ||
|
||
await waitFor(() => { | ||
expect(db.transaction).toBeDefined(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { View, Text } from 'react-native'; | ||
import { Dropdown } from 'react-native-element-dropdown'; | ||
import { FieldLabel } from '../support'; | ||
import { styles } from '../styles'; | ||
|
||
const TypeCascade = ({ onChange, values, keyform, id, name, source, dataSource = [] }) => { | ||
const [dropdownItems, setDropdownItems] = useState([]); | ||
|
||
const groupBy = (array, property) => { | ||
const gd = array | ||
.sort((a, b) => a?.name?.localeCompare(b?.name)) | ||
.reduce((groups, item) => { | ||
const key = item[property]; | ||
if (!groups[key]) { | ||
groups[key] = []; | ||
} | ||
groups[key].push(item); | ||
return groups; | ||
}, {}); | ||
const groupedData = {}; | ||
Object.entries(gd).forEach(([key, value]) => { | ||
groupedData[key] = value; | ||
}); | ||
return groupedData; | ||
}; | ||
|
||
const handleOnChange = (index, value) => { | ||
const nextIndex = index + 1; | ||
const updatedItems = dropdownItems | ||
.slice(0, nextIndex) | ||
.map((d, dx) => (dx === index ? { ...d, value } : d)); | ||
|
||
const options = dataSource?.filter((d) => d?.parent === value); | ||
|
||
if (options.length) { | ||
updatedItems.push({ | ||
options, | ||
value: null, | ||
}); | ||
} | ||
const dropdownValues = updatedItems.filter((dd) => dd.value).map((dd) => dd.value); | ||
const finalValues = updatedItems.length !== dropdownValues.length ? null : dropdownValues; | ||
onChange(id, finalValues); | ||
|
||
setDropdownItems(updatedItems); | ||
}; | ||
|
||
useEffect(() => { | ||
const parentID = source?.parent_id || 0; | ||
const filterDs = dataSource.filter( | ||
(ds) => | ||
ds?.parent === parentID || | ||
(values[id] && (values[id].includes(ds?.id) || values[id].includes(ds?.parent))), | ||
); | ||
|
||
if (dropdownItems.length === 0 && dataSource.length && filterDs.length) { | ||
const groupedDs = groupBy(filterDs, 'parent'); | ||
const initialDropdowns = Object.values(groupedDs).map((options, ox) => { | ||
return { | ||
options, | ||
value: values[id] ? values[id][ox] : null, | ||
}; | ||
}); | ||
setDropdownItems(initialDropdowns); | ||
} | ||
}, [dataSource, dropdownItems, source, values, id]); | ||
|
||
return ( | ||
<View testID="view-type-cascade"> | ||
<FieldLabel keyform={keyform} name={name} /> | ||
<Text testID="text-values" style={styles.cascadeValues}> | ||
{values[id]} | ||
</Text> | ||
<View style={styles.cascadeContainer}> | ||
{dropdownItems.map((item, index) => { | ||
return ( | ||
<Dropdown | ||
key={index} | ||
labelField="name" | ||
valueField="id" | ||
testID={`dropdown-cascade-${index}`} | ||
data={item?.options} | ||
onChange={({ id: selectedID }) => handleOnChange(index, selectedID)} | ||
value={item.value} | ||
style={[styles.dropdownField]} | ||
/> | ||
); | ||
})} | ||
</View> | ||
</View> | ||
); | ||
}; | ||
|
||
export default TypeCascade; |
Oops, something went wrong.