-
Notifications
You must be signed in to change notification settings - Fork 12
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 #13 from fram-x/feature/handle-undefined-text
Feature/handle undefined text
- Loading branch information
Showing
12 changed files
with
154 additions
and
27 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 { Mixed } from './parser'; | ||
|
||
const findAllTextStyles = ( | ||
mixedText: Mixed, | ||
accStyleNames: Array<string> = [] | ||
) : Array<string> => { | ||
mixedText.map(element => { | ||
if (!(typeof element === 'string' || element === undefined || element === null)) { | ||
const index = accStyleNames.indexOf(element.styleName); | ||
if (index < 0) { | ||
accStyleNames.push(element.styleName); | ||
} | ||
|
||
accStyleNames = findAllTextStyles(element.mixedText, accStyleNames); | ||
} | ||
}); | ||
|
||
return accStyleNames; | ||
} | ||
|
||
const verifyTextStyles = ( | ||
mixedText: Mixed, | ||
textStyles: Object, | ||
defaultStyles: Object | ||
) => { | ||
const styleNames = findAllTextStyles(mixedText); | ||
|
||
styleNames.forEach((styleName) => { | ||
if (!textStyles[styleName] && !defaultStyles[styleName]) { | ||
console.warn('react-native-styled-text: style "' + styleName + '" is not defined'); | ||
} | ||
}); | ||
} | ||
|
||
export { verifyTextStyles }; |
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,86 @@ | ||
import { verifyTextStyles } from '../StyledText/utils'; | ||
|
||
let warnings = []; | ||
const warn = msg => warnings.push(msg); | ||
const textStyles = { | ||
style1: {}, | ||
style2: {}, | ||
}; | ||
const defaultStyles = { | ||
b: {}, | ||
i: {}, | ||
u: {} | ||
}; | ||
|
||
describe('StyledText/utils', () => { | ||
beforeAll(() => { | ||
console['warn'] = warn; | ||
}); | ||
beforeEach(() => { | ||
warnings = []; | ||
}); | ||
|
||
describe('verifyTextStyles', () => { | ||
it('should not give warning on undefined', () => { | ||
const mixedText = [undefined]; | ||
verifyTextStyles(mixedText, textStyles, defaultStyles); | ||
expect(warnings.length).toBe(0); | ||
}); | ||
|
||
it('should not give warning on null', () => { | ||
const mixedText = [null]; | ||
verifyTextStyles(mixedText, textStyles, defaultStyles); | ||
expect(warnings.length).toBe(0); | ||
}); | ||
|
||
it('should not give warning on ""', () => { | ||
const mixedText = ['']; | ||
verifyTextStyles(mixedText, textStyles, defaultStyles); | ||
expect(warnings.length).toBe(0); | ||
}); | ||
|
||
it('should not give warning on "test"', () => { | ||
const mixedText = ['test']; | ||
verifyTextStyles(mixedText, textStyles, defaultStyles); | ||
expect(warnings.length).toBe(0); | ||
}); | ||
|
||
it('should not give warning on defined styles', () => { | ||
const mixedText = [ | ||
'test', | ||
{ | ||
styleName: 'style1', | ||
mixedText: [ | ||
'test2', | ||
{ | ||
styleName: 'b', | ||
mixedText: ['test3'] | ||
} | ||
] | ||
} | ||
]; | ||
verifyTextStyles(mixedText, textStyles, defaultStyles); | ||
expect(warnings.length).toBe(0); | ||
}); | ||
|
||
it('should not give warning on undefined styles', () => { | ||
const mixedText = [ | ||
'test', | ||
{ | ||
styleName: 'style3', | ||
mixedText: [ | ||
'test2', | ||
{ | ||
styleName: 'e', | ||
mixedText: ['test3'] | ||
} | ||
] | ||
} | ||
]; | ||
verifyTextStyles(mixedText, textStyles, defaultStyles); | ||
expect(warnings.length).toBe(2); | ||
expect(warnings[0]).toContain('"style3"'); | ||
expect(warnings[1]).toContain('"e"'); | ||
}); | ||
}); | ||
}); |