Sunday, January 31, 2021

how to use test object array contain element which match giving object?

 when we write a test case to verify that an object array contains specific object within JEST framework

we can use the following code to handle it .

let posts=[]

beforeAll(()=>{
    posts.push({title:'test'user:'dan'})
    posts.push({title:'hello world'user:'dan'})
})

test('test post exists',()=>{
    expect(posts).toEqual(
        expect.arrayContaining([
            expect.objectContaining({title: 'test'})
        ])
    )
})

Sunday, January 17, 2021

how to quickly fix "future version of React will block javascript: URLs as a security precaution. Use event handlers instead if you can. If you need to generate unsafe HTML try using dangerouslySetInnerHTML instead. React was passed "javascript:void(0)"

 when we implement a click event to trigger api call or dispatch an action in redux, we will like to use this simple syntax to handle it.

<a href="javascript:void(0)" onClick={clearFilter}>all posts</a>

we will see the following warming in the development tool windows


since React 16.9 update had deprecate the JavaScript URL, you can check out this link for more information

https://reactjs.org/blog/2019/08/08/react-v16.9.0.html

but the work around is quiet simple. we can fix it withe code below.

<a href="#" onClick={ev => {
            ev.preventDefault();
           clearFilter
            return false; // old browsers, may not be needed
            }}>all posts</a>