Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validations #77

Merged
merged 18 commits into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
'^.+\\.ts?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
modulePathIgnorePatterns: ['utils'],
setupFilesAfterEnv: [
"@testing-library/jest-native/extend-expect",
"./setup-tests.js"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"react-native-vector-icons": "^8.1.0",
"rn-fetch-blob-v2": "^0.14.1",
"upper-case-first": "^2.0.2",
"wollok-ts": "^3.0.6"
"wollok-ts": "^3.0.8"
},
"devDependencies": {
"@babel/core": "^7.12.9",
Expand Down
File renamed without changes.
115 changes: 115 additions & 0 deletions src/__tests__/Problems.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { render, RenderAPI } from '@testing-library/react-native'
import React from 'react'
import { Node, Problem } from 'wollok-ts/dist/model'
import { ProblemIcon } from '../components/problems/ProblemIcon'
import { ProblemReporterButton } from '../components/problems/ProblemReporterButton'
import { ProblemModal } from '../components/problems/ProblemsModal'
import { methodFQN } from '../utils/wollok-helpers'
import ProjectProviderMock from './utils/ProjectProviderMock'
import {
clazz,
error,
field,
method,
problem,
sentence,
singleton,
test,
warning,
wReturn,
} from './utils/wollokProject'

describe('ProblemIcon', () => {
it('is red on error', () => {
const rendered = render(<ProblemIcon problem={error} />)
expectErrorIcon(rendered)
})

it('is yellow on warning', () => {
const rendered = render(<ProblemIcon problem={warning} />)
expectWarningIcon(rendered)
})
})

describe('ProblemReporterButton', () => {
function renderProblemReporterButton(aNode: Node, ...problems: Problem[]) {
return render(
<ProjectProviderMock problems={problems}>
<ProblemReporterButton node={aNode} />
</ProjectProviderMock>,
)
}

it('should not be shown if there is no related problems', () => {
const rendered = renderProblemReporterButton(singleton)
expect(rendered.toJSON()).toBeNull()
})

it('should show error if there is related warnings and problems', () => {
const rendered = renderProblemReporterButton(
singleton,
warning,
problem(singleton),
)
expectErrorIcon(rendered)
})

describe('should render if there is any problem', () => {
function iconExistTest(name: string, node: Node, problem: Problem) {
it(name, () => {
const rendered = renderProblemReporterButton(node, problem)
expect(rendered.toJSON()).not.toBeNull()
})
}

iconExistTest('with problem node', singleton, warning)

iconExistTest('inside method body', method, error)

iconExistTest('inside test body', test, problem(test.sentences()[0]))

iconExistTest('inside return expression', wReturn, problem(wReturn.value!))
})
})

describe('ProblemModal', () => {
function renderProblemModal(...problems: Problem[]) {
return render(
<ProblemModal
problems={problems}
visible={true}
setVisible={jest.fn()}
onSelect={jest.fn()}
/>,
)
}

describe('should render description for', () => {
function problemDescriptionTest(
name: string,
description: string,
problem: Problem,
) {
it(name, () => {
const { getByText } = renderProblemModal(problem)
expect(getByText(description)).toBeDefined()
})
}

problemDescriptionTest('singleton', singleton.name!, warning)
problemDescriptionTest('class', clazz.name!, problem(clazz))
problemDescriptionTest('field', field.name, problem(field))
problemDescriptionTest('method', methodFQN(method), problem(method))
problemDescriptionTest('sentence', methodFQN(method), problem(sentence))
})
})

function expectErrorIcon({ UNSAFE_getByProps }: RenderAPI) {
expect(
UNSAFE_getByProps({ icon: 'alert-circle', color: 'red' }),
).toBeDefined()
}

function expectWarningIcon({ UNSAFE_getByProps }: RenderAPI) {
expect(UNSAFE_getByProps({ icon: 'alert', color: 'yellow' })).toBeDefined()
}
24 changes: 13 additions & 11 deletions src/__tests__/TestItem-test.tsx → src/__tests__/TestItem.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react'
import { fireEvent, render } from '@testing-library/react-native'
import TestItem from '../components/tests/TestItem'
import { Body, Test } from 'wollok-ts/dist/model'
import { TestRun } from '../utils/wollok-helpers'
import React from 'react'
import { ActivityIndicator } from 'react-native-paper'
import { act, ReactTestInstance } from 'react-test-renderer'
import { Body, Test, WollokException } from 'wollok-ts'
import TestItem from '../components/tests/TestItem'
import { theme } from '../theme'
import { WollokException } from 'wollok-ts/dist/interpreter/runtimeModel'
import { TestRun } from '../utils/wollok-helpers'
import ProjectProviderMock from './utils/ProjectProviderMock'

const testMock = new Test({
name: 'TEST',
Expand Down Expand Up @@ -48,12 +48,14 @@ const renderTest = (runner: () => TestRun = jest.fn()) => {
UNSAFE_queryByProps,
queryByText,
} = render(
<TestItem
item={testMock}
runner={runner}
onClick={jest.fn()}
theme={theme}
/>,
<ProjectProviderMock>
<TestItem
item={testMock}
runner={runner}
onClick={jest.fn()}
theme={theme}
/>
</ProjectProviderMock>,
)
return {
runIcon: () => UNSAFE_getByProps({ icon: 'play-circle' }),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { Literal, Variable as VariableModel } from 'wollok-ts/dist/model'
import { renderWithTheme } from '../../../../../utils/test-helpers'
import { Variable } from '../Variable'
import { renderWithTheme } from './utils/test-helpers'
import { Variable } from '../components/Body/sentences/Variable'

function renderVariable(variable: VariableModel) {
const { UNSAFE_queryByProps, getByText } = renderWithTheme(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
Sentence,
Variable as VariableModel,
} from 'wollok-ts/dist/model'
import { renderWithTheme } from '../../../../../utils/test-helpers'
import { Assignment } from '../Assignment'
import { Return } from '../Return'
import { Send } from '../Send'
import { Variable } from '../Variable'
import { VisualSentence } from '../VisualSentence'
import { Assignment } from '../components/Body/sentences/Assignment'
import { Return } from '../components/Body/sentences/Return'
import { Send } from '../components/Body/sentences/Send'
import { Variable } from '../components/Body/sentences/Variable'
import { VisualSentence } from '../components/Body/sentences/VisualSentence'
import { renderWithTheme } from './utils/test-helpers'

describe('matching sentences with components', () => {
it('should match a send sentence', () => {
Expand Down
36 changes: 36 additions & 0 deletions src/__tests__/utils/ProjectProviderMock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react'
import { Environment, Problem } from 'wollok-ts/dist/model'
import { ProjectContext } from '../../context/ProjectProvider'
import { ParentComponentProp } from '../../utils/type-helpers'

export const initialContext = {
project: new Environment({ members: [] }),
name: 'Project Test',
changed: false,
problems: [] as Problem[],
actions: {
addEntity: jest.fn(),
addDescribe: jest.fn(),
addMember: jest.fn(),
changeMember: jest.fn(),
rebuildEnvironment: jest.fn(),
runTest: jest.fn(),
execution: jest.fn(),
save: jest.fn(),
},
}

type InitialProject = Partial<typeof initialContext>

const ProjectProviderMock = ({
children,
...props
}: ParentComponentProp<InitialProject>) => {
return (
<ProjectContext.Provider value={Object.assign(initialContext, props)}>
{children}
</ProjectContext.Provider>
)
}

export default ProjectProviderMock
14 changes: 14 additions & 0 deletions src/__tests__/utils/test-helpers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { NavigationContainer } from '@react-navigation/native'
import { render } from '@testing-library/react-native'
import React from 'react'
import { theme } from '../../theme'
import ProjectProviderMock from './ProjectProviderMock'
import { OneOrMany } from '../../utils/type-helpers'

export function renderWithTheme(children: OneOrMany<JSX.Element>) {
return render(
<ProjectProviderMock>
<NavigationContainer theme={theme}>{children}</NavigationContainer>
</ProjectProviderMock>,
)
}
125 changes: 125 additions & 0 deletions src/__tests__/utils/wollokProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import {
Body,
Class,
Describe,
Environment,
Expression,
Field,
Import,
link,
Literal,
Method,
Node,
Package,
Parameter,
Problem,
Reference,
Return,
Send,
Singleton,
Test,
WRE,
} from 'wollok-ts'
import { fromJSON } from 'wollok-ts/dist/jsonUtils'

const main = new Package({
name: 'main',
members: [
new Singleton({
name: 'pepita',
members: [
new Field({
name: 'energia',
isConstant: false,
isProperty: true,
value: new Literal({ value: 100 }),
}),
new Method({
name: 'come',
parameters: [
new Parameter({
name: 'comida',
}),
],
body: new Body({
sentences: [
new Send({
receiver: new Reference({ name: 'comida' }),
message: 'energiaQueAporta',
}),
],
}),
}),
],
}),
new Class({
name: 'Entrenador',
}),
],
})

const tests = new Package({
name: 'tests',
imports: [
new Import({
entity: new Reference({ name: 'main' }),
isGeneric: true,
}),
],
members: [
new Describe({
name: 'Main Describe',
members: [
new Test({
id: 'TEST',
name: 'test for testing',
body: new Body({
sentences: [
new Send({
receiver: new Reference({ name: 'assert' }),
message: 'that',
args: [new Literal({ value: true })],
}),
],
}),
}),
],
}),
],
})

export const project = link([main, tests], fromJSON<Environment>(WRE))

export const singleton = project.getNodeByFQN('main.pepita') as Singleton

export const clazz = project.getNodeByFQN('main.Entrenador') as Singleton

export const describe = project.getNodeByFQN('tests.Main Describe') as Describe

export const field = singleton.members[0] as Field

export const method = singleton.members[1] as Method

export const test = describe.members[0] as Test

export const sentence = method.sentences()[0]

export const wReturn = new Return({ value: sentence as Expression })

// PROBLEMS

export const problem = (node: Node): Problem => ({
node,
code: 'ERROR',
level: 'error',
values: [],
})

export const error = problem(sentence)

export const warning: Problem = {
node: singleton,
code: 'SINGLETON_WARNING',
level: 'warning',
values: [],
}
Loading